A purity change

Maxim Fomin maxim at maxim-fomin.ru
Thu Jul 11 09:58:43 PDT 2013


On Thursday, 11 July 2013 at 15:25:10 UTC, bearophile wrote:
> This used to compile (probably dmd 2.060):
>
>
> struct Foo {
>     immutable int y;
>     void bar(TF)(TF f) pure {
>         f(1);
>     }
>     void spam() pure {
>         bar((int x) => y);
>     }
> }
> void main() {}
>
>
> But now it gives:
>
> test.d(4): Error: pure function 'test.Foo.bar!(immutable(int) 
> delegate(int x) nothrow @safe).bar' cannot call impure delegate 
> 'f'
> test.d(7): Error: template instance 
> test.Foo.bar!(immutable(int) delegate(int x) nothrow @safe) 
> error instantiating
>
> Is it a correct error?
>
> Bye,
> bearophile

My guess is that before 2.063 immutable int y was implicitly 
static, but now it is per instance data which requires context 
pointer which is deduced to be impure - that why compilation 
fails (try placing static attribute). In other words, before 
2.063 this was a function and now it is a delegate. And delegates 
currently don't play well with constness/immutability.


More information about the Digitalmars-d-learn mailing list