Invariant doesn't apply to declared symbols

Jason House jason.james.house at gmail.com
Fri Nov 30 10:03:29 PST 2007


Walter Bright wrote:

> Jason House wrote:
>> This is the one thing that really bugs me about const...
>> <snip>
> It's much simpler:
> 
> const(S) s; // can rebind s
> const S s;  // cannot rebind s

First of all, the docs don't explicitly talk about this.  That really HAS to
get fixed.

Also, "const(char)*a" and "const(char*)a" having different reference types
confuses me.  In both cases, I can assign to a, but can't assign to *a. 
They really look the same.  This should be documented too.

Probably on a related note, "const(char) a" allows assignment to a
(it's "rebindable"), but as far as I can tell, value types simply get
overwritten when changed.  I'd argue that such a definition is useless,
confusing, error-prone, and should be a compile-time error.

Maybe the problem is that the storage class specification is too easily
confused with other const declarations.  As much as it may draw tossing of
rotten fruit at me, I think we need the final keyword.

Would it be safe to say that "const int* x;" and "final const(int*) x;" are
the same thing?  Similarly that "invariant int* y;" and "final
invariant(int*) y;" are the same thing?

I'd argue that const and invariant should never be a storage class.  At the
risk of even more rotten fruit, this aligns well with the C++
interpretation of "const char*" as a function parameter.  Of course, if a
function has a return type of const(char*), it makes total sense that it
should be assignable to a variable.  I think it all works and should, in
the end, be less confusing.



More information about the Digitalmars-d mailing list