Selftype: an idea from Scala for D2.0
eao197
eao197 at intervale.ru
Fri May 25 21:59:02 PDT 2007
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?
--
Regards,
Yauheni Akhotnikau
More information about the Digitalmars-d
mailing list