Let's not make invariants const

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Aug 3 12:36:41 PDT 2012


On Fri, Aug 03, 2012 at 09:19:22PM +0200, Alex Rønne Petersen wrote:
[...]
> 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
[...]
> I believe this behavior is too strict.
[...]

IMO, if you need to be changing stuff inside invariants, then you're
using it wrong. Invariants are intended to verify program logic, not to
do things like altering object state. The point is to be able to compile
with invariant code turned off, and still have the program work exactly
as before.


T

-- 
Be in denial for long enough, and one day you'll deny yourself of things you wish you hadn't.


More information about the Digitalmars-d mailing list