Replies: 3 comments 5 replies
-
This should already be the case. |
Beta Was this translation helpful? Give feedback.
-
Here the destructor is called after "end" is printed: module dtor;
import std.stdio;
struct S
{
~this(){print("hi");}
}
void f(S s)
{
auto c = cast(char)(s.i+'0');
[c].print;
}
void main()
{
S s;
s.f;
"end".print;
} Output:
I expected the destructor to be also called at the end of I would like the destructor to only be called at the end of |
Beta Was this translation helpful? Give feedback.
-
See my comment on the struct constructor bug for why this is nontrivial. The constructor semantics for function calls are "a little funky". It is vital to understand that copy constructor and destructor calls are inseparable from Neat's reference counting. In short, here the call to If you write For a lot of examples for how this works, see the Lifetime testcase. (Note that the refcounting has a lot of bugs! If something looks fishy, err on the side of reporting it.) |
Beta Was this translation helpful? Give feedback.
-
Hi,
I think initialization is an area where Neat could break away from D. D has no support for non-null references to avoid accidental segfaults. Part of that problem is that D requires every type to have an
.init
value. This also means any struct destructor has to handle the.init
case (even if@disable this();
is used, callingmove
ordestroy
will write theinit
value). It also means the destructor can be called more than once unnecessarily.Could Neat instead not default initialize but detect when an uninitialized variable is used? cppfront does this. It also moves/destroys a struct on last use in the current scope. So rather than copying a struct passed by value when it's not used after that, it moves and does not call the destructor in the current scope.
Beta Was this translation helpful? Give feedback.
All reactions