[Issue 15862] Functions that return types with mutable indirections should be weakly pure, not strongly pure

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Oct 14 14:38:59 PDT 2016


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

--- Comment #10 from ag0aep6g at gmail.com ---
(In reply to Walter Bright from comment #7)
> Yeah, my mistake. The compiler should consider @nogc when marking a pure
> function as a common subexpression.

I think approaching this with @nogc would be a mistake. Considering mutable
indirections in the return type seems superior to me.

You can have a function that allocates via the GC but doesn't return any
mutable indirections. Either it allocates for internal use only, or it casts to
immutable when returning. You can reuse the result of such a function for
another identical call.

I'm only rehashing David Nadlinger's article on this matter, of course.
http://klickverbot.at/blog/2012/05/purity-in-d/

Considering the indirections in the return type would also leave the door open
for other allocators to be used in `pure` code. Tying this to @nogc would only
add to the arbitrary divide between the GC and others. As far as I see, the GC
isn't actually more pure than other allocators, it's just recognized by the
language which makes it seem natural to special case it.

--


More information about the Digitalmars-d-bugs mailing list