praveenvvstgy / crack-language

Automatically exported from code.google.com/p/crack-language
Other
0 stars 0 forks source link

Const parameters and const method #93

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,
because all objects are passed to function by reference, it would be great to 
be able to specify const parameter to disallow a function to modify an object.

Second, const object may be modified, which is unwanted.
So, const method would be useful to disallow modifying const object.

For instance, lets say we have a classe representing a number:
class Number {
    int _x;

    oper init(int x) : _x = x {

    }

    void print() {
        cout `$_x\n`;
    }

    void modify(int x) {
        _x = x;
    }
}

I am able to declare a const Number object and to change it without error:
const Number num = {20};
num.print();
num.modify(21);
num.print();

In brief, it would be nice to have const parameters and const method.

Original issue reported on code.google.com by boua...@gmail.com on 17 Dec 2011 at 3:58

GoogleCodeExporter commented 9 years ago
The "const" keyword has a different meaning in Crack than in C++.  In Crack, it 
simply means "the variable can not be assigned."  It was introduced for 
primitive values to prevent the risk of numeric constants in modules being 
modified and to allow us the optimization of using those constants by value, as 
opposed to by loading a global variable.

The language will never have anything like const variables and methods like in 
C++ because this feature is widely considered problematic.  I would be open to 
discussion on better ways to facilitate immutable objects, but any such 
discussion should be hashed out on the mailing list.

Original comment by mind...@gmail.com on 19 Dec 2011 at 2:40