Bug 9551 – template this parameter not recognized in constructors
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-20T07:36:56Z
Last change time
2018-03-09T18:10:12Z
Assigned to
No Owner
Creator
Gor Gyolchanyan
Comments
Comment #0 by gor.f.gyolchanyan — 2013-02-20T07:36:56Z
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 ')'
Comment #1 by k.hara.pg — 2013-02-20T08:13:53Z
(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)`.
Comment #2 by gor.f.gyolchanyan — 2013-02-20T08:15:30Z
(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.
Comment #3 by k.hara.pg — 2013-02-20T08:29:17Z
(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.
Comment #4 by gor.f.gyolchanyan — 2013-02-20T08:31:09Z
(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.
Comment #5 by k.hara.pg — 2013-02-20T08:37:45Z
(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?
Comment #6 by gor.f.gyolchanyan — 2013-02-20T08:39:22Z
(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.
Comment #7 by k.hara.pg — 2013-02-20T08:51:50Z
(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.
Comment #8 by schveiguy — 2013-02-20T09:01:14Z
(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.
Comment #9 by k.hara.pg — 2013-02-20T09:15:10Z
(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.
Comment #10 by nick — 2018-03-09T18:10:12Z
(In reply to Gor Gyolchanyan from comment #0)
> Base b = new Derived;
> b(); // 100% transparent dynamic dispatch
The code compiles with dmd v2.077.1 (if you add a dummy argument above, to match the signature, e.g. b(4)).