digitalmars.D.learn - Struct constructors callable twice?
- Guilherme Vieira <n2.nitrogen gmail.com> Jan 12 2011
- bearophile <bearophileHUGS lycos.com> Jan 12 2011
- Guilherme Vieira <n2.nitrogen gmail.com> Jan 12 2011
--001636284880d7e5700499a4947d
Content-Type: text/plain; charset=ISO-8859-1
I've found this behavior while toying with opCall() in a struct:
import std.stdio;
struct Struct
{
this(int value) { writeln("Struct.this(", value, ")"); }
~this() { writeln("Struct.~this()"); }
}
void main()
{
Struct s = Struct(1); // prints `Struct.this(1)`
s(2); // prints `Struct.this(2)`
s(3); // prints `Struct.this(3)`
} // prints `Struct.~this()`
Notice how the destructor is only called once. If there was an opCall
defined for Struct, its reconstruction would shadow it. It certainly looks
like a bug to me, but since I'm "sure of nothing" in D I decided to post it
here. Is it really a bug?
--
Atenciosamente / Sincerely,
Guilherme ("n2liquid") Vieira
--001636284880d7e5700499a4947d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<font class=3D"Apple-style-span" face=3D"arial, helvetica, sans-serif">I=
9;ve found this behavior while toying with opCall() in a struct:</font><div=
<font class=3D"Apple-style-span" face=3D"arial, helvetica, sans-serif"><br=
</font><blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0 0=
<span class=3D"Apple-style-span" style=3D"font-family: 'courier new'=
;, monospace; ">import std.stdio;</span><font class=3D"Apple-style-span" fa=
ce=3D"'courier new', monospace"><br></font><span class=3D"Apple-sty=
le-span" style=3D"font-family: 'courier new', monospace; ">struct S=
truct<br>
</span><span class=3D"Apple-style-span" style=3D"font-family: 'courier =
new', monospace; ">{<br></span><span class=3D"Apple-style-span" style=
=3D"font-family: 'courier new', monospace; ">=A0=A0 =A0this(int val=
ue) { writeln("Struct.this(", value, ")"); }<br>
</span><span class=3D"Apple-style-span" style=3D"font-family: 'courier =
new', monospace; ">=A0=A0 =A0~this() { writeln("Struct.~this()&quo=
t;); }<br></span><span class=3D"Apple-style-span" style=3D"font-family: =
9;courier new', monospace; ">}</span><font class=3D"Apple-style-span" f=
ace=3D"'courier new', monospace"><br>
</font><span class=3D"Apple-style-span" style=3D"font-family: 'courier =
new', monospace; ">void main()<br></span><span class=3D"Apple-style-spa=
n" style=3D"font-family: 'courier new', monospace; ">{<br></span><s=
pan class=3D"Apple-style-span" style=3D"font-family: 'courier new',=
monospace; ">=A0=A0 =A0Struct s =3D Struct(1); // prints `Struct.this(1)`<=
br>
</span><span class=3D"Apple-style-span" style=3D"font-family: 'courier =
new', monospace; ">=A0=A0 =A0s(2); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0//=
prints=A0</span><span class=3D"Apple-style-span" style=3D"font-family: =
9;courier new', monospace; ">`</span><span class=3D"Apple-style-span" s=
tyle=3D"font-family: 'courier new', monospace; ">Struct.this(2)`</s=
pan></blockquote>
<blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0 0 0 40px;=
border: none; padding: 0px;"><span class=3D"Apple-style-span" style=3D"fon=
t-family: 'courier new', monospace; ">=A0=A0 =A0s(3);=A0=A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0=A0// prints=A0</span><span class=3D"Apple-style-span" =
style=3D"font-family: 'courier new', monospace; ">`</span><span cla=
ss=3D"Apple-style-span" style=3D"font-family: 'courier new', monosp=
ace; ">Struct.this(3)`</span></blockquote>
<blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0 0 0 40px;=
border: none; padding: 0px;"><font class=3D"Apple-style-span" face=3D"'=
;courier new', monospace">} =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 // prints=A0</font><span class=3D"Apple-style-span" style=3D"font-fami=
ly: 'courier new', monospace; ">`</span><font class=3D"Apple-style-=
span" face=3D"'courier new', monospace">Struct.~this()</font><span =
class=3D"Apple-style-span" style=3D"font-family: 'courier new', mon=
ospace; ">`</span></blockquote>
<div><br></div><div>Notice how the destructor is only called once. If there=
was an opCall defined for Struct, its reconstruction would shadow it. It c=
ertainly looks like a bug to me, but since I'm "sure of nothing&qu=
ot; in D I decided to post it here. Is it really a bug?</div>
</div><div><div><br></div>-- <br>Atenciosamente / Sincerely,<br>Guilherme (=
"n2liquid") Vieira<br>
</div>
--001636284880d7e5700499a4947d--
Jan 12 2011
See also: http://d.puremagic.com/issues/show_bug.cgi?id=4053 Bye, bearophile
Jan 12 2011
--0016e64cbf6cfe2e940499a5cc3d
Content-Type: text/plain; charset=ISO-8859-1
Yes, it was meddling with that bug that I cooked this one.
I think Adam got it right: "There should be no problem between an instance
opCall and a constructor". In your case, it seems, a static opCall was
clashing with the constructor. But it turns out a non-static opCall clashes
with a constructor that AFAIK shouldn't even be there (see my initial post).
--
Atenciosamente / Sincerely,
Guilherme ("n2liquid") Vieira
On Wed, Jan 12, 2011 at 10:12 AM, bearophile <bearophileHUGS lycos.com>wrote:
See also:
http://d.puremagic.com/issues/show_bug.cgi?id=4053
Bye,
bearophile
--0016e64cbf6cfe2e940499a5cc3d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Yes, it was meddling with that bug that I cooked this one.<div><br></div><d=
iv>I think Adam got it right:<font class=3D"Apple-style-span" face=3D"arial=
, helvetica, sans-serif"> "<span class=3D"Apple-style-span" style=3D"w=
hite-space: pre-wrap; -webkit-border-horizontal-spacing: 1px; -webkit-borde=
r-vertical-spacing: 1px; ">There should be no problem between an instance o=
pCall and a constructor". In your case, it seems, a static opCall was =
clashing with the constructor. But it turns out a non-static opCall clashes=
with a constructor that AFAIK shouldn't even be there (see my initial =
post).</span></font><div>
<div><br></div><div>--=A0<br>Atenciosamente / Sincerely,<br>Guilherme (&quo=
t;n2liquid") Vieira</div><div><br><br><div class=3D"gmail_quote">On We=
d, Jan 12, 2011 at 10:12 AM, bearophile <span dir=3D"ltr"><<a href=3D"ma=
ilto:bearophileHUGS lycos.com">bearophileHUGS lycos.com</a>></span> wrot=
e:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex;">See also:<br>
<a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D4053" target=3D"=
_blank">http://d.puremagic.com/issues/show_bug.cgi?id=3D4053</a><br>
<br>
Bye,<br>
<font color=3D"#888888">bearophile</font></blockquote></div>
</div></div></div>
--0016e64cbf6cfe2e940499a5cc3d--
Jan 12 2011









bearophile <bearophileHUGS lycos.com> 