Bug 17714 – Function template - this T for static methods

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2017-08-03T14:00:00Z
Last change time
2017-08-04T05:16:36Z
Assigned to
nobody
Creator
timosesu
See also
https://issues.dlang.org/show_bug.cgi?id=14191

Comments

Comment #0 by timosesu — 2017-08-03T14:00:09Z
For static method templates the this T will not work to deduce the calling derived class/interface. Although this information is present at compile-time, there seems to be no way to easily access the calling derived class: interface I { static void test(this T)() { writeln(T.type.stringof); } } abstract class A { static void test(this T)() { writeln(T.type.stringof); } } class B : A { alias type = uint; } class C : I { alias type = int; } void main() { B.test(); C.test(); } Throws: Error: template app.A.test cannot deduce function from argument types !()(), candidates are: app.A.test(this T)() Error: template app.I.test cannot deduce function from argument types !()(), candidates are: app.I.test(this T)() Shouldn't it be possible to access the class which is calling the static method as easily as "this T" as template parameter? Would "this T" be too confusing, as it is a static method and this does not really exist? To simplify, the following should be possible in some way (if not via "this T"): class A { // T will be whatever class is calling this method static void func (this T)() { }; } A.func(); Possibly related issue: https://issues.dlang.org/show_bug.cgi?id=14191
Comment #1 by schveiguy — 2017-08-03T17:08:37Z
Huh, I think separately Timon Gehr added an almost identical issue right before you :) *** This issue has been marked as a duplicate of issue 17713 ***
Comment #2 by timosesu — 2017-08-04T05:16:36Z
(In reply to Steven Schveighoffer from comment #1) > Huh, I think separately Timon Gehr added an almost identical issue right > before you :) > > *** This issue has been marked as a duplicate of issue 17713 *** That's quite a coincidence : D. Forum post discussion: https://forum.dlang.org/post/[email protected]