pureity of closures

Meta via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 30 08:17:19 PDT 2015


On Monday, 30 March 2015 at 13:18:44 UTC, Dicebot wrote:
> On Sunday, 29 March 2015 at 12:29:13 UTC, Stefan Koch wrote:
>> On Friday, 27 March 2015 at 17:47:26 UTC, H. S. Teoh wrote:
>>> What I'm more concerned about is whether the current compiler
>>> implementation may accidentally allow leakage of the pure 
>>> function's
>>> internal context, which would break purity.
>>>
>>>
>>> T
>> please explain your reasoning with a bit of example code.
>> I am not sure if I get where/when impurity would be introduced.
>
> void delegate() metafoo() pure
> {
>     int x;
>     return () { x = 42; }; // now stack frame of pure function
>                            // is available externally
>                            // no idea what may happen
> }

Maybe even better:

void delegate() metafoo pure
{
     int x;
     if (x > 0)
     {
         return () => x -= 1;
     }
     else
     {
         return () => x += 1;
     }
}


More information about the Digitalmars-d mailing list