[Issue 14781] impure delegate to pure function context should be able to modify context

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jul 8 12:33:23 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14781

ag0aep6g at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ag0aep6g at gmail.com

--- Comment #6 from ag0aep6g at gmail.com ---
(In reply to Denis Shelomovskij from comment #5)
> Then this function isn't strongly pure anymore:
> ---
> T f() pure;
> ---
> if `T` is `int delegate()`.
> 
> Is everybody OK with this?

I think that's already not strongly pure. `int delegate()` has a mutable
indirection in the context pointer. So a function that returns such a delegate
can't be strongly pure.

See
http://klickverbot.at/blog/2012/05/purity-in-d/#indirections_in_the_return_type

An example with the delegate type:
----
struct S
{
    int x = 1;
    int method() {return x++;}
}

int delegate() f() pure
{
    return &(new S).method;
}

void main()
{
   auto dg1 = f();
   version(all) auto dg2 = f();
   else auto dg2 = dg1; /* This would be equivalent to the above if f were
strongly pure. */

   import std.stdio;
   writeln(dg1()); /* "1" */
   writeln(dg1()); /* "2" */
   writeln(dg2()); /* "1" or "3", depending on the version above */
}
----

--


More information about the Digitalmars-d-bugs mailing list