pureity of closures

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 23 02:45:39 PDT 2015


On Sunday, 22 March 2015 at 12:58:25 UTC, Stefan Koch wrote:
> dmd infers function closures impure if impure functions are 
> defined within them.
> even if those are never called and can never be accessed 
> outside of the closure.
>
> Example :
>
> int a;
> void closure() pure {
>   impure_function() {
>     a++;
>    }
> }
> t.d(4): Error: pure function 't.closure.impure_function' cannot 
> access mutable static data 'a'
>
> is this intended ?

I think this was not intended and is simply a side effect of 
limited D call graph analysis. Relaxing that limitation makes 
sense to me because unused impure function can be used for 
compile-time reflection or returned from pure function as a 
result (you need to ensure it does not capture pure functions as 
closure context in that case)


More information about the Digitalmars-d mailing list