May 30, 2017 Re: Value closures (no GC allocation) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vittorio Romeo | On Sunday, 21 May 2017 at 00:33:30 UTC, Vittorio Romeo wrote: > Hello everyone, I recently started learning D (I come from a Modern C++ background) and I was curious about closures that require GC allocation. I wrote this simple example: > > auto bar(T)(T x) @nogc > { > return x(10); > } > > auto foo(int x) @nogc > { > return bar((int y) => x + y + 10); > } > > int main() @nogc > { > return foo(10); > } > > > > It doesn't compile with the following error: > > Error: function example.foo is @nogc yet allocates closures with the GC > example.foo.__lambda2 closes over variable x at [...] > > > > Live example on godbolt: https://godbolt.org/g/tECDh4 > > > > I was wondering whether or not D could provide some syntax that allowed the user to create a "value closure", similar to how C++ lambdas work. How would you feel about something like: > > > auto bar(T)(T x) @nogc > { > return x(10); > } > > auto foo(int x) @nogc > { > return bar([x](int y) => x + y + 10); > } > > int main() @nogc > { > return foo(10); > } > > > > The syntax: > > [x](int y) => x + y + 10 > > would mean "create a 'value closure' that captures `x` by value inside it". It would be equivalent to the following program: > > struct AnonymousClosure > { > int captured_x; > > this(int x) @nogc > { > captured_x = x; > } > > auto opCall(int y) @nogc > { > return captured_x + y + 10; > } > } > > auto foo(int x) @nogc > { > return bar(AnonymousClosure(x)); > } > > > > Which is very similar to how C++ lambdas work. This would allow closures to be used in @nogc contexts with minimal syntactical overhead over classical closures. > > Live example on godbolt: https://godbolt.org/g/ML2dlP > > What are your thoughts? Has something similar been proposed before? https://wiki.dlang.org/DIP30 Also, while no syntax is provided, this is how SDC works internally and this is how it can handle multiple context pointers. |
June 04, 2017 Re: Value closures (no GC allocation) | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Tuesday, 30 May 2017 at 19:29:38 UTC, deadalnix wrote:
>> What are your thoughts? Has something similar been proposed before?
>
> https://wiki.dlang.org/DIP30
>
> Also, while no syntax is provided, this is how SDC works internally and this is how it can handle multiple context pointers.
FWIW, I think your three DIPs are elegant. Aside from questions of backward compatibility with current D, I like the way you designed functions and delegates there. Does this design integrate with the OP of this thread? It seems to. That's a question for the author of this thread too, if he reads this.
|
June 04, 2017 Re: Value closures (no GC allocation) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vittorio Romeo | On Sunday, 21 May 2017 at 00:33:30 UTC, Vittorio Romeo wrote: > Hello everyone, I recently started learning D (I come from a Modern C++ background) and I was curious about closures that require GC allocation. I wrote this simple example: > > [...] I started to do some changes to the compiler. https://github.com/MakersF/dmd/tree/valuelambda It's my first time messing with DMD, it's mostly to familiarize myself with the compiler, so I don't expect to do everything the right way. Let me know if you have inputs. |
Copyright © 1999-2021 by the D Language Foundation