www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Type of receiver

reply Jacob Carlborg <doob me.com> writes:
When the template this parameter feature was added to the language it 
was possible to use it for static methods:

class Foo
{
     static void foo(this T)()
     {
         pragma(msg, T.stringof);
     }
}

class Bar : Foo {}

Foo.foo(); // prints "Foo"
Bar.foo(); // prints "Bar"

For some reason this feature was removed, it might be that it was never 
indented to work for static methods.

Can we make it work again for static methods? It would be really handy 
for creating Objective-C bindings.

-- 
/Jacob Carlborg
Oct 24 2015
next sibling parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Saturday, 24 October 2015 at 09:54:43 UTC, Jacob Carlborg 
wrote:
 When the template this parameter feature was added to the 
 language it was possible to use it for static methods:

 class Foo
 {
     static void foo(this T)()
     {
         pragma(msg, T.stringof);
     }
 }

 class Bar : Foo {}

 Foo.foo(); // prints "Foo"
 Bar.foo(); // prints "Bar"

 For some reason this feature was removed, it might be that it 
 was never indented to work for static methods.

 Can we make it work again for static methods? It would be 
 really handy for creating Objective-C bindings.
It was changed in this PR: https://github.com/D-Programming-Language/dmd/pull/1687 It's hard to tell whether it was intentional though. But IMO your code should work, so I suggest you file a bug report.
Oct 24 2015
parent Jacob Carlborg <doob me.com> writes:
On 2015-10-24 15:38, Marc Sch├╝tz wrote:

 It was changed in this PR:
 https://github.com/D-Programming-Language/dmd/pull/1687

 It's hard to tell whether it was intentional though. But IMO your code
 should work, so I suggest you file a bug report.
I did that, back in 2013 [1]. It was closed as invalid. [1] https://issues.dlang.org/show_bug.cgi?id=10488 -- /Jacob Carlborg
Oct 24 2015
prev sibling parent reply bitwise <bitwise.pvt gmail.com> writes:
On Saturday, 24 October 2015 at 09:54:43 UTC, Jacob Carlborg 
wrote:
 When the template this parameter feature was added to the 
 language it was possible to use it for static methods:

 class Foo
 {
     static void foo(this T)()
     {
         pragma(msg, T.stringof);
     }
 }

 class Bar : Foo {}

 Foo.foo(); // prints "Foo"
 Bar.foo(); // prints "Bar"

 For some reason this feature was removed, it might be that it 
 was never indented to work for static methods.

 Can we make it work again for static methods? It would be 
 really handy for creating Objective-C bindings.
I was looking at this a couple of days ago. It would be really useful if I could do this: class TypeInfo {} class TypeInfoImpl(T) : TypeInfo {} class BaseObject { static TypeInfo typeInfo(this This)() { return TypeInfoImpl!This(); } static TypeInfo info = typeInfo(); } class Foo : BaseObject { // also has 'info' with correct info for 'Foo'. } So, all subclasses of 'BaseObject' would get the appropriate type info. I would be using 'BaseObject' as a node in a scene graph. So everyone who defined new objects to go in the scene graph would have the appropriate type info added to their class without having to override some virtual function manually. Bit
Oct 24 2015
parent bitwise <bitwise.pvt gmail.com> writes:
On Saturday, 24 October 2015 at 16:05:15 UTC, bitwise wrote:
 [...]
 class TypeInfo {}
 class TypeInfoImpl(T) : TypeInfo {}

 class BaseObject {
     static TypeInfo typeInfo(this This)() {
         return TypeInfoImpl!This();
     }

     static TypeInfo info = typeInfo();
 }
 class Foo : BaseObject {
     // also has 'info' with correct info for 'Foo'.
 }
 [...]
     Bit
Realizing now that this was a little bit of a brain fart... My point is though, that it would be awesome if we could have functionality automatically generated by the compiler for all subclasses...but have it useable as a virtual method. Maybe: class Base { /* virtual function that is automatically instantiated for all subclasses */ synthesized void foo(this This)() { doSomething!This; } } class Derived : Base { // foo automatically overridden for this class. } Bit
Oct 24 2015