Let's not make invariants const

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


On 03-08-2012 21:19, Alex Rønne Petersen wrote:
> 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?
>

I should also mention that invariants weren't const until very recently, 
so this is also quite the breaking change...

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


More information about the Digitalmars-d mailing list