Selftype: an idea from Scala for D2.0

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Sat May 26 05:53:57 PDT 2007


eao197 wrote:

> On Sat, 26 May 2007 08:21:48 +0400, David B. Held
> <dheld at codelogicconsulting.com> wrote:
> 
>> In general, this is equivalent to the CRTP in C++, which has the general
>> form in D of:
>>
>> class Derived : RecurringBase!(Derived)
>> { ... }
>>
>> As with any recurrence pattern, you have to be careful about what is
>> allowed and what isn't, or you may end up with an infinitely nested
>> definition.  For instance, if RecurringBase(T) is allowed to derive from
>> T, you get the regress.  However, D prevents this because T is a forward
>> reference at instantiation time.
>>
>> I'm not sure what the value-add of the "self type" is when the CRTP is
>> available in D:
>>
>> class Base(T) {      T x; }
>> class Derived : Base!(Derived) { int x; }
>> void main() { Derived d; }
>>
>> This is a perfectly valid D program according to dmd.
> 
> Could you rewrite my initial example with Subject/Observer with using CRTP
> technique?

You're using it already in the first post. The two classes are packaged
inside a single template so it might not seem so obvious.



More information about the Digitalmars-d mailing list