www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct constructors callable twice?

reply Guilherme Vieira <n2.nitrogen gmail.com> writes:
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
Jan 12 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
See also:
http://d.puremagic.com/issues/show_bug.cgi?id=4053

Bye,
bearophile
Jan 12 2011
parent Guilherme Vieira <n2.nitrogen gmail.com> writes:
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
Jan 12 2011