digitalmars.D.learn - Does the 'with' statement affect object lifetime?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (54/54) Jun 19 2012 Is the following code legal for a class?
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (9/61) Jun 19 2012 I say bug.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/8) Jun 19 2012 Ah... Good point.
- Timon Gehr (2/3) Jun 19 2012 Bug.
Is the following code legal for a class?
class C {
int i;
}
void main() {
with(new C()) {
i = 42; // <-- Is the anonymous object alive at this point?
}
}
The object seems to live long enough for a class. That's probably
because the garbage collector kicks in late. But consider the same code
with a struct:
import std.stdio;
struct S {
int i;
this(int i = 0)
{
writeln("constructed");
}
~this()
{
writeln("destructed");
}
}
void main() {
with(S(1)) {
writeln("inside 'with' statement");
i = 42; // <-- Is the anonymous object alive at this point?
}
}
The output indicates that the anonymous object is destroyed before the
body of the with is executed:
constructed
destructed
inside 'with' statement
This contradicts with with's spec:
http://dlang.org/statement.html#WithStatement
It says that
with (expression)
{
...
ident;
}
is semantically equivalent to:
{
Object tmp;
tmp = expression;
...
tmp.ident;
}
Bug?
Ali
--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Jun 19 2012
On 19-06-2012 19:51, Ali Çehreli wrote:
Is the following code legal for a class?
class C {
int i;
}
void main() {
with(new C()) {
i = 42; // <-- Is the anonymous object alive at this point?
}
}
The object seems to live long enough for a class. That's probably
because the garbage collector kicks in late. But consider the same code
with a struct:
import std.stdio;
struct S {
int i;
this(int i = 0)
{
writeln("constructed");
}
~this()
{
writeln("destructed");
}
}
void main() {
with(S(1)) {
writeln("inside 'with' statement");
i = 42; // <-- Is the anonymous object alive at this point?
}
}
The output indicates that the anonymous object is destroyed before the
body of the with is executed:
constructed
destructed
inside 'with' statement
This contradicts with with's spec:
http://dlang.org/statement.html#WithStatement
It says that
with (expression)
{
...
ident;
}
is semantically equivalent to:
{
Object tmp;
tmp = expression;
...
tmp.ident;
}
Bug?
Ali
I say bug.
BTW, the reason the class reference is alive is because it's on the
stack. Regardless of whether you're using with, it wouldn't be collected
by the GC until the current stack frame exits.
--
Alex Rønne Petersen
alex lycus.org
http://lycus.org
Jun 19 2012
On 06/19/2012 10:56 AM, Alex Rønne Petersen wrote:BTW, the reason the class reference is alive is because it's on the stack. Regardless of whether you're using with, it wouldn't be collected by the GC until the current stack frame exits.Ah... Good point. Here is the bug report: http://d.puremagic.com/issues/show_bug.cgi?id=8269 Ali
Jun 19 2012
On 06/19/2012 07:51 PM, Ali Çehreli wrote:Bug?Bug.
Jun 19 2012









=?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> 