Challenge

anonymous via Digitalmars-d digitalmars-d at puremagic.com
Sun Aug 30 06:36:44 PDT 2015


On Sunday 30 August 2015 12:21, rsw0x wrote:

> import std.algorithm, std.range;
> 
> auto foo(R)(R a, immutable int b)
> {
>      return a.map!(x => x + b);
> }
> 
> @nogc @safe unittest
> {
>      int[3] test = [1,2,3];
> 
>      assert(test[].foo(3).equal(only(4,5,6)));
> }
> 
> does this count?

I think this shouldn't compile and it only does so because of issue 14771.

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

The delegate in foo uses a local variable and it's returned from the 
function, so it needs a closure. Where does the closure go if not on the GC 
heap?

See also this Learn thread about how to call map with a delegate in a @nogc 
function:

http://forum.dlang.org/post/kpwbtskhnkkiwkdsfzby@forum.dlang.org

Ali Çehreli's solution there may or may not actually be @nogc, but I'm 
afraid it relies on issue 14771 any way:

http://forum.dlang.org/post/mqr506$10kf$1@digitalmars.com


More information about the Digitalmars-d mailing list