[Issue 17645] `pure` is transitively applied to all delegates inside a pure function

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jul 17 08:27:07 PDT 2017


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

--- Comment #4 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to Tomer Filiba (weka) from comment #3)
> `format` does GC and isn't pure.

`format` actually is pure.

void main() pure
{
   auto str = format("oh no x=%s", 5);
}

But you are right that it's not @nogc. However, this isn't the focus of the bug
report (or is it?). In your new example, you are actually calling the
GC-allocating function from inside a @nogc context, not creating a delegate.
Even if you did create a delegate and needed to save the context, that creates
a closure, and that definitely needs the GC.

> the property itself is perfectly pure and
> @nogc and whatnot, but by adding an assert i have to remove these attributes
> from it.

I understand. This is more like an enhancement request to have assert ignore
the requirements of pure and @nogc for the lazy evaluated arg.

> maybe, but it means i can't write it in the same statement, i.e. this won't
> work
> 
>     assumePure({x++});

As I said earlier, I think the fact that the literal isn't inferred to be a
function instead of a delegate is a bug. but you can force a function literal
out of this:

int x;
auto foo() pure
{
   return function() { x++; }; // should be able to omit 'function'
}

void main()
{
   foo()();
}

So you have a few choices here:

1. Update the bug to say that these should be inferred as functions (even if
you don't do this, another bug should be filed).
2. Argue that a delegate with a context of a pure function should be able to be
unpure, and demonstrate why that's needed with an updated use case.
3. Alter the bug report to mean something different.

--


More information about the Digitalmars-d-bugs mailing list