Clay language

Simen kjaeraas simen.kjaras at gmail.com
Thu Dec 30 17:03:23 PST 2010


Michel Fortin <michel.fortin at michelf.com> wrote:


> I stubbled upon this yesterday:
>
> 	Template This Parameters
>
> 	TemplateThisParameters are used in member function templates to pick up  
> the type of the this reference.
> 	import std.stdio;
>
> 	struct S
> 	{
> 		const void foo(this T)(int i)
> 		{
> 			writeln(typeid(T));
> 		}
> 	}
>
> <http://www.digitalmars.com/d/2.0/template.html>
>
> Looks like you could return the type of this this way...

The problem of template this parameters is that it doesn't work as one
might expect:

class A {
     void bar( this T )( ) {
         writeln( typeid( T ) );
     }
}

class B : A {
}

void main( ) {
     A a = new B;
     a.bar();
}

Surely this will print modulename.B, right?

Wrong. It prints modulename.A, as A is the type of the reference
 from which the function is called. It looks like a way to automagically
override a function for each subclass, but no.


-- 
Simen


More information about the Digitalmars-d mailing list