November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5056


Witold Baryluk <baryluk@smp.if.uj.edu.pl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |baryluk@smp.if.uj.edu.pl


--- Comment #10 from Witold Baryluk <baryluk@smp.if.uj.edu.pl> 2010-11-21 19:07:23 PST ---
> > This confuses me. It works both in D1 and D2. Maybe it's a regression? The spec says:
> > 
> > "3. It is illegal to refer to this implicitly or explicitly prior to making a
> > constructor call."
> > http://www.digitalmars.com/d/1.0/class.html#constructors
> 
> I interpret that the same way you do.   I think maybe that this is a rule that was removed, but someone forgot to remove it from the docs?  I think you should file a separate doc bug on it.
> 
> It doesn't strike me as a good requirement anyways.  What problems does having that restriction prevent?

Hmm. I was using this "feature", setting fields before calling super(), for VERY long time. It worked, so i never actually payed attention, if i call super() at the beginning or the end of constructor - both worked well.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5056


Michal Minich <michal.minich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |michal.minich@gmail.com


--- Comment #11 from Michal Minich <michal.minich@gmail.com> 2013-06-05 07:26:54 PDT ---
dmd 2.063 - instance method is called before constructor.

module test;
import std.stdio;

class Base
{
    this () {
       writeln("Base.this");
       foo(); // here should come warning: calling virtual method in
constructor
    }

    void foo () { writeln("Base.foo"); }
}

class Derived : Base
{
    this () { writeln("Derived.this"); }
    override void foo () { writeln("Derifed.foo"); }
}

void main () { auto d = new Derived; }

Program output:
Base.this
Derifed.foo    // method Derived.foo is called before object constructor
Derived.this

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5056


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #12 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-06-05 07:53:35 PDT ---
(In reply to comment #11)
> dmd 2.063 - instance method is called before constructor.

There's a simple fix for this, simply call super() directly whenever you want
to:

class Derived : Base
{
    this() { writeln("Derived.this"); super(); }
    override void foo () { writeln("Derifed.foo"); }
}

Result:

Derived.this
Base.this
Derifed.foo

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5056



--- Comment #13 from Michal Minich <michal.minich@gmail.com> 2013-06-05 08:16:45 PDT ---
(In reply to comment #12)
> (In reply to comment #11)
> > dmd 2.063 - instance method is called before constructor.
> 
> There's a simple fix for this, simply call super() directly whenever you want

Slightly better, but hardly a fix. at best partial workaround. you exchange one invalid initialization order for other invalid order - now Derived is initialized before Base. I guess is a less of a problem as developer of Derived can handle this invalid order locally.

Think of Base and Derived to be developed by independent developers not knowing the details of implementation:

In current scheme, to ensure that derived class is initialized before any overridden method can be called: developer has to explicitly call the super() constructor as the last step in derived constructor. And that must be done for every derived class on which any method is overridden. Also this would not only for the most-derived class, if there is more derived classes in hierarchy - every one hast to do it.

Two sides of problem are:

- Developer of Base might not be aware that method called in constructor is virtual, so he accidentally allows it to be hijacked.

- Developer of Derived might be not aware that method  he has overridden is called in Base constructor, which makes his overridden method to be allays called before his constructor his called.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
1 2
Next ›   Last »