Re: Variables with scoped destruction in closures

2016-10-16 Thread ketmar via Digitalmars-d-learn
On Saturday, 15 October 2016 at 07:55:30 UTC, ketmar wrote: p.s. compiler doesn't complain each time, only in some circumstances. i don't remember the exact code now, but some of it has nothing to do with closures at all -- no std.algo, no templates with lambda args, etc.

Re: Variables with scoped destruction in closures

2016-10-16 Thread ketmar via Digitalmars-d-learn
On Saturday, 15 October 2016 at 05:41:05 UTC, Walter Bright wrote: The problem is the closure is generated when it is expected that the delegate will survive past the end of the scope (it's the whole point of a closure). But with a destructor that runs at the end of the scope, it cannot survive

Re: Variables with scoped destruction in closures

2016-10-15 Thread Walter Bright via Digitalmars-d-learn
On 10/14/2016 3:18 AM, Nordlöw wrote: t_scope.d(23,6): Error: variable t_scope.below.s has scoped destruction, cannot build closure https://github.com/dlang/dmd/blob/master/src/toir.d#L820 The problem is the closure is generated when it is expected that the delegate will survive past the en

Re: Variables with scoped destruction in closures

2016-10-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/14/16 6:18 AM, Nordlöw wrote: The following code import std.algorithm.iteration : filter; import std.algorithm.mutation : move; import std.range : iota; static private struct S { import core.memory : GC; @disable this(this); this(int x) { _ptr = cast(typeof(_ptr)

Re: Variables with scoped destruction in closures

2016-10-14 Thread ag0aep6g via Digitalmars-d-learn
On Friday, 14 October 2016 at 14:00:53 UTC, ag0aep6g wrote: As for ways to make this work: 1) You can move s to the heap yourself: [...] 2) Or you can move it into a struct that gets returned (more involved): [...] 3) Put a struct on the heap that acts as the closure: auto below5(size_

Re: Variables with scoped destruction in closures

2016-10-14 Thread Nordlöw via Digitalmars-d-learn
On Friday, 14 October 2016 at 16:37:06 UTC, Nordlöw wrote: If I remove the explicit dtor from `S` the code compiles. Can That of course being incorrect D code that leaks memory.

Re: Variables with scoped destruction in closures

2016-10-14 Thread Nordlöw via Digitalmars-d-learn
On Friday, 14 October 2016 at 14:00:53 UTC, ag0aep6g wrote: As for ways to make this work: 1) You can move s to the heap yourself: auto below3(size_t n, S s = S.init) { import std.algorithm.mutation: moveEmplace; auto onHeap = cast(S*) new ubyte[S.sizeof]; moveEmplace(s, *onHea

Re: Variables with scoped destruction in closures

2016-10-14 Thread ag0aep6g via Digitalmars-d-learn
On 10/14/2016 12:18 PM, Nordlöw wrote: import std.algorithm.iteration : filter; import std.algorithm.mutation : move; import std.range : iota; static private struct S { import core.memory : GC; @disable this(this); this(int x) { _ptr = cast(typeof(_ptr))GC.malloc((*_ptr)

Re: Variables with scoped destruction in closures

2016-10-14 Thread Nordlöw via Digitalmars-d-learn
On Friday, 14 October 2016 at 10:18:19 UTC, Nordlöw wrote: t_scope.d(23,6): Error: variable t_scope.below.s ... Correction, should be: t_scope.d(23,6): Error: variable t_scope.below1.s ...

Variables with scoped destruction in closures

2016-10-14 Thread Nordlöw via Digitalmars-d-learn
The following code import std.algorithm.iteration : filter; import std.algorithm.mutation : move; import std.range : iota; static private struct S { import core.memory : GC; @disable this(this); this(int x) { _ptr = cast(typeof(_ptr))GC.malloc((*_ptr).sizeof); *