On 7/17/2012 12:23 PM, Jonathan M Davis wrote:
On Tuesday, July 17, 2012 14:48:32 David Nadlinger wrote:
On Tuesday, 17 July 2012 at 05:24:26 UTC, Jonathan M Davis wrote:
This code strikes me as being a bug:

--------
class MyBase(T)
{}

class MySubA : MyBase!MySubA
{}

class MySubB : MyBase!MySubB
{}

void main()
{}
--------

This pattern is actually quite common in C++ code, and referred
to as CRTP (curiously recurring template pattern). If you propose
to kill it, Andrei is going to get mad at you. ;)

Well, it certainly seems insane to me at first glance - particularly when you
take compile time reflection into account, since the derived classes'
definitions are now effectively recursive (so I suspect that the situation is
worse in D, since C++ doesn't have conditional compliation like D does). But
if it's supposed to be legal, I guess that it's suppose to be legal. I'd never
seen the idiom before, and it seemed _really_ off to me, which is why I brought
it up. But I'd have to study it in order to give an informed opinion on it.

- Jonathan M Davis


A 'proper' D port of this kind design would be to use mixins instead of the template. They both accomplish the same thing:

The template (or mixins) are written to call functions in the user defined type. A simple example would be the C++ WTL library: A user defined control defines its own window style, but the template code is responsible for creating the window, and accesses the style and class flags from the user defined type.

The advantage is the same in both: you avoid making the interface virtual, you still get to use some generic code.

Reply via email to