final invariant request = disallow meaningless attributes

Deewiant deewiant.doesnotlike.spam at gmail.com
Sat Jun 23 11:06:01 PDT 2007


With the new constants, people, including many who have used D for a long time,
are more confused than ever before about what certain combinations of attributes
mean. The compiler still accepts interesting code like:

export public public private protected package int apple;

I think this has become a real issue now that the difference, or lack thereof,
between the following isn't obvious:

const           int x = 3;
invariant       int x = 3;
const invariant int x = 3;

As far as I understand, the above are equivalent. Yet the compiler accepts all
three. This is confusing to people who don't fully understand the various
semantics of const, final, and invariant. (I didn't include final above because
I'm not sure myself, but I suppose final would be equivalent as well.)

This has been requested before, but I raise the issue now again - the compiler
should disallow applying attributes that:

1) make no sense in the context, like "private" for a function's local variable
2) override one another, like "public private" or "const invariant" anywhere
3) have an equivalent alternative in the context, like const as a storage class

The third point can be debated, as it requires enforcing a standard, such as
"const int x = 3" over "invariant int x = 3".

As I understand it, the only problem with this would be having to write
special-case code for templates. For instance, if final would only be applicable
to reference types, one would have to duplicate entire templates, with only
minor changes for value types.

For that, I suggest that the first point of the three I mentioned would be
disregarded if the declaration is for a templated type.

I'm not sure if this would solve all the problems - it seems too simple. But I
do think DMD/D getting a bit stricter in this regard would be very helpful.

-- 
Remove ".doesnotlike.spam" from the mail address.



More information about the Digitalmars-d mailing list