A purity change

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jul 11 09:12:40 PDT 2013


On Thu, Jul 11, 2013 at 05:25:09PM +0200, 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?
[...]

Hmm. This seems to be a tricky corner case. The delegate itself is
impure, as it accesses y which is outside of its definition and isn't
part of its arguments; however, in the larger context of spam(), this
shouldn't be a problem since y is accessed via 'this', and 'this' is
part of spam's input.

I'm not sure how feasible it is for the compiler to statically check
such cases, though. But IMO we should at least look into this.


T

-- 
Why can't you just be a nonconformist like everyone else? -- YHL


More information about the Digitalmars-d-learn mailing list