Is This a Solution For the Const/Rebindable Issue?

Mehrdad wfunction at hotmail.com
Sun Jun 12 20:36:21 PDT 2011


An idea came to my mind for fixing the const/rebindable issue, and I'm 
not sure if it's a good idea or not but I thought I'd mention it. I'm 
not sure if it's been mentioned before, but a quick search didn't make 
it seem like it has been.

Right now, the trouble seems to be from the fact that a piece of code like:
     const(Object) obj;
prevents obj from being assigned a new object.

For the solution: Why not just relax this restriction for reference 
types? After all, references are just pretty much pointers, and it's not 
we disallow pointers to const value types from being rebindable, right?
Value types (e.g. structs) would still not be rebindable (that makes 
sense, since it would really be changing the value) but reference types 
like Object would be rebindable by default.

### NOTEt: This will **NOT** break any existing code! ###

We could then allow the "final" keyword to be used for members that are 
reference types (but this would NOT be allowed for value types), to 
simply prevent their rebinding (although they need not be const).

Examples:
     int i; //variable
     const int j = 5; //constant
     final int k; //compiler ERROR (should say const instead, since int 
is a value type)
     const Object obj1; //rebindable reference, but contents are all const
     final Object obj2; //non-rebindable reference to a regular object
     final const Object obj3; //non-rebindable reference to a const object

Does this sound like a good idea?


More information about the Digitalmars-d mailing list