Type of receiver

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Sat Oct 24 09:05:10 PDT 2015


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



More information about the Digitalmars-d mailing list