Let's not make invariants const

Simen Kjaeraas simen.kjaras at gmail.com
Fri Aug 3 12:32:10 PDT 2012


On Fri, 03 Aug 2012 21:19:22 +0200, Alex Rønne Petersen <alex at lycus.org>  
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?

Votes++

In a perfect world...

-- 
Simen


More information about the Digitalmars-d mailing list