Thread overview
[Issue 4413] New: typeof(this) doesn't work in method template signature
Feb 06, 2011
Iain Buclaw
Mar 31, 2011
klickverbot
Mar 31, 2011
Iain Buclaw
Dec 21, 2011
Kenji Hara
Dec 21, 2011
Kenji Hara
Dec 27, 2011
Walter Bright
Apr 24, 2012
Kenji Hara
July 01, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4413

           Summary: typeof(this) doesn't work in method template signature
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-07-01 14:27:43 PDT ---
dmd v2.047 shows a compilation error on the call to bar4, but in my opinion the compiler has to compile all four those bar methods:


struct Foo {
    alias typeof(this) typeof_this;
    void bar1(typeof_this other) {}
    void bar2()(typeof_this other) {}
    void bar3(typeof(this) other) {}
    void bar4()(typeof(this) other) {}
}
void main() {
    Foo f;
    f.bar1(f); // OK
    f.bar2(f); // OK
    f.bar3(f); // OK
    f.bar4(f); // ERR
}


The generated errors:
test.d(13): Error: template test.Foo.bar4() does not match any function
template declaration
test.d(13): Error: template test.Foo.bar4() cannot deduce template function
from argument types !()(Foo)


The problem of bar4 has shown up in D2 code similar to this one, where I have used typeof(this) to follow the DRY strategy and avoid repeating the struct name more than one time:

struct Vec2 {
    float x, y;
    auto opBinary(string op)(typeof(this) other) if (op == "+") {
      ...
    }
}


A workaround that can be used is:

struct Vec2 {
    float x, y;
    alias typeof(this) typeof_this;
    auto opBinary(string op)(typeof_this other) if (op == "+") {
      ...
    }
}

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


Iain Buclaw <ibuclaw@ubuntu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw@ubuntu.com


--- Comment #1 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-02-06 07:32:03 PST ---
Calling semantic on the param type in ::deduceType seems to be simplest workaround without causing side effects.


--- a/src/template.c
+++ b/src/template.c
@@ -2183,6 +2183,7 @@ MATCH TypeStruct::deduceType(Scope *sc, Type *tparam,
TemplateParameters *parame
      * to a template instance, too, and try again.
      */
     TemplateInstance *ti = sym->parent->isTemplateInstance();
+    tparam = tparam->semantic(0, sc);

     if (tparam && tparam->ty == Tinstance)
     {
@@ -2317,6 +2318,7 @@ MATCH TypeClass::deduceType(Scope *sc, Type *tparam,
TemplateParameters *paramet
      * to a template instance, too, and try again.
      */
     TemplateInstance *ti = sym->parent->isTemplateInstance();
+    tparam = tparam->semantic(0, sc);

     if (tparam && tparam->ty == Tinstance)
     {


Ideally though, all typeof()'s should have been expanded beforehand.

Regards

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


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |akb825@gmail.com


--- Comment #2 from bearophile_hugs@eml.cc 2011-02-06 11:58:12 PST ---
*** Issue 5532 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 31, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4413


klickverbot <code@klickverbot.at> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code@klickverbot.at


--- Comment #3 from klickverbot <code@klickverbot.at> 2011-03-30 17:53:08 PDT ---
The patch by Iain seems to break std.datetime:

/usr/local/include/d2/std/datetime.d(9217): Error: template instance
std.datetime.DTRebindable!(immutable(TimeZone)) error instantiating

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 31, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4413



--- Comment #4 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-03-31 05:43:42 PDT ---
(In reply to comment #3)
> The patch by Iain seems to break std.datetime:
> 
> /usr/local/include/d2/std/datetime.d(9217): Error: template instance
> std.datetime.DTRebindable!(immutable(TimeZone)) error instantiating

Yep, I was merely suggesting that things should ideally be worked out before this point (because they *can* be worked out).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4413



--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2011-12-21 05:55:25 PST ---
*** Issue 5801 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4413


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
           Platform|x86                         |All
         OS/Version|Windows                     |All


--- Comment #6 from Kenji Hara <k.hara.pg@gmail.com> 2011-12-21 05:55:58 PST ---
https://github.com/D-Programming-Language/dmd/pull/572

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4413


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> 2011-12-26 18:26:35 PST ---
https://github.com/D-Programming-Language/dmd/commit/8fa99fca73fea3a5130939ecaa83596f925cdbb8

https://github.com/D-Programming-Language/dmd/commit/2324c9903f99bb355ac5b2f25298a4f05ab0fba1

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4413


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm@gmail.com


--- Comment #8 from Kenji Hara <k.hara.pg@gmail.com> 2012-04-23 17:04:51 PDT ---
*** Issue 5903 has been marked as a duplicate of this issue. ***

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