Thread overview
[Issue 9551] New: template this parameter not recognized in constructors
Feb 20, 2013
Gor Gyolchanyan
Feb 20, 2013
Kenji Hara
Feb 20, 2013
Gor Gyolchanyan
Feb 20, 2013
Kenji Hara
Feb 20, 2013
Gor Gyolchanyan
Feb 20, 2013
Kenji Hara
Feb 20, 2013
Gor Gyolchanyan
Feb 20, 2013
Kenji Hara
Feb 20, 2013
Kenji Hara
February 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9551

           Summary: template this parameter not recognized in constructors
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: gor.f.gyolchanyan@gmail.com


--- Comment #0 from Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> 2013-02-20 07:36:56 PST ---
The template this parameter, which works fine for normal methods:

    class Base
    {
        string label;

        this(string label_)
        {
            label = label_;
        }

        string toString(this This_)()
        {
            return This_.stringof ~ "(`" ~ label ~ "`)";
        }
    }

    class Derived: Base
    {
        this()
        {
            super("Hello, world!");
        }
    }

    unittest
    {
        Derived d = new Derived();
        assert(d.toString() == "Derived(`Hello, world!`)");
    }

doesn't work for constructors:

    class Base
    {
        this(this This_)()
        {
            // Build a dispatch table using __traits(allMethods, This_)
        }

        void opCall(Payload_)(Payload_ payload_)
        {
            // Dynamically dispatch payload_ using the previously built
dispatch table.
        }
    }

    class Derived: Base
    {
        // implicitly generated constructor will call the Base.this() and
implicitly give it the static type of Derived.
    }

    unittest
    {
        Base b = new Derived;
        b(); // 100% transparent dynamic dispatch
    }


Because of the following compile-time errors:

    C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: found 'This_' when
expecting ')'
    C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: semicolon expected
following function declaration
    C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: Declaration expected, not
')'

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



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2013-02-20 08:13:53 PST ---
(In reply to comment #0)
> Because of the following compile-time errors:
> 
>     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: found 'This_' when
> expecting ')'
>     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: semicolon expected
> following function declaration
>     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: Declaration expected, not
> ')'

As far as I see, it conflicts with postblit syntax `this(this)`.

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



--- Comment #2 from Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> 2013-02-20 08:15:30 PST ---
(In reply to comment #1)
> (In reply to comment #0)
> > Because of the following compile-time errors:
> > 
> >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: found 'This_' when
> > expecting ')'
> >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: semicolon expected
> > following function declaration
> >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: Declaration expected, not
> > ')'
> 
> As far as I see, it conflicts with postblit syntax `this(this)`.

Yes, it definitely looks like it, but the existence of a type name after "this" in the template parameter list should disambiguate it.

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



--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2013-02-20 08:29:17 PST ---
(In reply to comment #2)
> (In reply to comment #1)
> > (In reply to comment #0)
> > > Because of the following compile-time errors:
> > > 
> > >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: found 'This_' when
> > > expecting ')'
> > >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: semicolon expected
> > > following function declaration
> > >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: Declaration expected, not
> > > ')'
> > 
> > As far as I see, it conflicts with postblit syntax `this(this)`.
> 
> Yes, it definitely looks like it, but the existence of a type name after "this" in the template parameter list should disambiguate it.

But, it is not just only a parser problem.
Constructor will be treated specially for object construction entry. And,
unfortunately, language semantics between non-mutable object construction and
qualified constructor is yet not defined well. The combination of
TemplateThisParameter and constructor may touch similar *yet not defined
semantics*.

Therefore, I must say that this issue will not be fix soon.

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



--- Comment #4 from Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> 2013-02-20 08:31:09 PST ---
(In reply to comment #3)
> (In reply to comment #2)
> > (In reply to comment #1)
> > > (In reply to comment #0)
> > > > Because of the following compile-time errors:
> > > > 
> > > >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: found 'This_' when
> > > > expecting ')'
> > > >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: semicolon expected
> > > > following function declaration
> > > >     C:\Users\g.gyolchanyan\Desktop\test.d(3): Error: Declaration expected, not
> > > > ')'
> > > 
> > > As far as I see, it conflicts with postblit syntax `this(this)`.
> > 
> > Yes, it definitely looks like it, but the existence of a type name after "this" in the template parameter list should disambiguate it.
> 
> But, it is not just only a parser problem.
> Constructor will be treated specially for object construction entry. And,
> unfortunately, language semantics between non-mutable object construction and
> qualified constructor is yet not defined well. The combination of
> TemplateThisParameter and constructor may touch similar *yet not defined
> semantics*.
> 
> Therefore, I must say that this issue will not be fix soon.

This is extremely unfortunate, because this is the only way that I know of to implement transparent dynamic dispatch.

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



--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2013-02-20 08:37:45 PST ---
(In reply to comment #4)
> 
> This is extremely unfortunate, because this is the only way that I know of to implement transparent dynamic dispatch.

Couldn't you use helper function for the construction?

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



--- Comment #6 from Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> 2013-02-20 08:39:22 PST ---
(In reply to comment #5)
> (In reply to comment #4)
> > 
> > This is extremely unfortunate, because this is the only way that I know of to implement transparent dynamic dispatch.
> 
> Couldn't you use helper function for the construction?

Theoretically, I could, but it would require immense boilerplate when dealing with large class hierarchies, which is hard to enforce.

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



--- Comment #7 from Kenji Hara <k.hara.pg@gmail.com> 2013-02-20 08:51:50 PST ---
(In reply to comment #6)
> (In reply to comment #5)
> > (In reply to comment #4)
> > > 
> > > This is extremely unfortunate, because this is the only way that I know of to implement transparent dynamic dispatch.
> > 
> > Couldn't you use helper function for the construction?
> 
> Theoretically, I could, but it would require immense boilerplate when dealing with large class hierarchies, which is hard to enforce.

I cannot imagine the 'immense boilerplate', but sorry it would need for a while.

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


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com


--- Comment #8 from Steven Schveighoffer <schveiguy@yahoo.com> 2013-02-20 09:01:14 PST ---
(In reply to comment #1)

> As far as I see, it conflicts with postblit syntax `this(this)`.

Does it?  postblit does not have the extra parentheses which is the signature of a template method.  I'm asking out of ignorance.

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



--- Comment #9 from Kenji Hara <k.hara.pg@gmail.com> 2013-02-20 09:15:10 PST ---
(In reply to comment #8)
> (In reply to comment #1)
> 
> > As far as I see, it conflicts with postblit syntax `this(this)`.
> 
> Does it?  postblit does not have the extra parentheses which is the signature of a template method.  I'm asking out of ignorance.

I'm saying that _currently_ they are confused, and should be properly distinguished.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------