On Thu, 30 Dec 2010 21:14:28 -0500, Simen kjaeraas
<simen.kja...@gmail.com> wrote:
Andrej Mitrovic <andrej.mitrov...@gmail.com> wrote:
On 12/31/10, Simen kjaeraas <simen.kja...@gmail.com> wrote:
This will give you both:
class A
{
void bar(this T) ( )
{
writeln(typeid(T));
writeln(typeid(this));
}
}
class B : A
{
}
void main( )
{
A a = new B;
a.bar();
}
Indeed it will. Now, if you look closely, you will see that typeid(T) is
A, while typeid(this) is B. Testing further:
class A {
void baz() {
writeln(typeid(this));
}
}
class B : A {
}
void main() {
A a = new B;
a.baz(); // prints B.
}
We thus see that the template this parameter has absolutely no value.
No, it does have value:
class A
{
string x;
T setX(this T, U)(U newx)
{
this.x = to!string(newx);
return cast(T)this;
}
}
class B : A
{
int y;
void setY(int newy)
{
this.y = newy;
}
}
void main()
{
auto b = new B;
b.setX(5).setY(6);
}
Hey, look! covariance with templates :)
Now if only templates worked in interfaces...
I also have to write a helper function to eliminate that cast (which does
a runtime lookup).
-Steve