Let's not make invariants const

Alex Rønne Petersen alex at lycus.org
Fri Aug 3 12:19:22 PDT 2012


Hi,

This:

$ cat test.d
class A
{
     int i;

     invariant()
     {
         i = 42;
     }
}

Currently doesn't compile:

$ dmd test.d
test.d(7): Error: can only initialize const member i inside constructor

(Obviously this example is silly, but it's just meant to illustrate the 
point of this thread.)

I believe this behavior is too strict. I don't agree that the language 
should dictate what *my* invariant can and cannot do. Not to mention 
that the standard library is far from const-friendly enough for this 
strictness to be practically reasonable today (I have tons of cast()s in 
my programs today due to this - not cool).

Does anyone else find this behavior too strict?

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d mailing list