My thoughts on the potential of the curiously recurring template pattern in D
See more atthe announce forum.
If you have default behavior that the user can override, a common solution is to use a class with runtime polymorphism. The rest of your code can simply use the base class and the user can customize the behavior at construction time by providing an instance of their class instead. D supports this paradigm well, at least when the compile-time structure is known.
But, what if there is compile time knowledge needed? For example, if you are working with arbitrary, user-defined types:
1classBase {2/* virtual */voidprocess(T)(Tt) {/* default behavior */ }3}45structMyData {}67classChild :Base {8// not gonna work :(9overridevoidprocess(T :MyData)(Tt) { }10}
Withstatic foreach, we could define individual virtual overloads for a closed set of types, but that cannot expand to new types. With runtime variadics and delegates, we can handle new data types, but lose compile-time information in the base class implementation. Regular virtual methods won't help much.
We could use a template mixin with default implementations, and delegate to it:
1mixintemplateDefaultProcessors() {2voidprocess(T) {}3}45classChild :Base {6mixinDefaultProcessorsdp;7voidprocess(T :MyData)(Tt) {}8voidprocess(T)(Tt) {dp.process(t); }// needed for overload set resolution9}
Great! ... but the base class itself can't see any of that. You can pass Child instances to templates that can see it, but Base isn't one of those.
But, what if it was?
classBase(CRTP) {}classChild :Base!Child {}
This is called the "curiously recurring template pattern", or CRTP. It was (actually pretty accidentally) discovered in C++ in the 90's and lets the base class see its own child at compile time, providing the possibility of static polymorphism.
With this, we don't need the mixin template anymore, and can instead put the default behavior directly into the base class.
1classBase(CRTP) {2voidprocess(T)(Tt) {importstd.stdio;writeln("default process ",T.stringof); }3voidprocess(T :int)(Tt) {importstd.stdio;writeln("specialized base int process"); }4voidsomeTraditionalMethod() {5 (cast(CRTP)this).process("my string");6 (cast(CRTP)this).process(0);7 (cast(CRTP)this).process([1]);8 }9}1011classChild :Base!Child {12voidprocess(T :string)(Tt) {13importstd.stdio;writeln("custom string process!");14 }15voidprocess(T)(Tt) {super.process(t); }// still necessary to remind the compiler we do still want default fallback processing16}1718voidmain() {19autochild =newChild();20child.someTraditionalMethod();21}
Now we have a mix of arbitrary template specialization and overriding as well as traditional OOP inheritance. We can provide a mix of methods and templates to let the user control the whole class' behaviour and it is open to any new types we need to pass while keeping all compile time data available. Pretty cool stuff.
The downside though is the D language will not let us actually use theoverride keyword on templates, meaning you must get the names and arguments right or face either silent, unintended behavor (you got the name wrong) or loud, unhelpful error messages out of dmd itself (you got the arguments wrong, but the errors will apper to come from insideBase and barely even tell you what is actually expected). As a base class author using this technique, you'll have to be sure to carefully document the signatures and help your users along, reminding them of the necessity of the generic forwarder function too.
Thankfully, ddoc processors will be able to help your users to some extent, since at least the pseudo-virtual functions will be listed inside the base class too, giving you a reasonable place to write those docs.
Moreover, it is possible to have the base class use compile-time reflection to provide better error messages for its own children. Perhaps a user-defined@Override attribute could help catch such mismatches withstatic assert as well. I haven't had time to play with this myself yet, but I can't think of a reason why it wouldn't work, though I don't think it would be quite as nice as we get for runtime virtual polymorphism.
Another thing to keep in mind is that if you forget to castthis to the child class in a base, you won't actually call the "overridden" function. I can't think of any way to help with this except to say "don't do that". (You can also call your regular virtuals through the cast too, and by doing so possibly help the compiler in devirtualizing those calls for a small performance boost.)
And lastly, if the base class isn't written with this in mind, the pattern will not work. You want to suggest your users make the child classesfinal. But there is still the possibility of overriding other virtual methods and using regular substitution, so this may be an unnecessary restriction in some cases.
In any case, I had never found a use for the CRTP in the past with my own code, but am now exploring the possibilities of it in the new web framework I'm working on and I think there is a lot of potential that I don't see other D programmers doing much with. Here's hoping I'm actually seeing something they haven't :)
If you have any good uses of it, or superior alternatives, I wanna hear! Make a thread on forum.dlang.org, ping me (adam_d_ruppe) in #d on freenode IRC, or email me destructionator@gmail.com and I should see it and we can chat.
The tip of the week came out of me wanting to solve this problem for the new web framework: how can I provide sane out-of-the-box defaults for working with arbitrary user types while still letting the user change that behavior?
Of course, there are a lot of options. I could decorate types with user-defined attributes and have the reflection react to them. I could check for the existence of special methods on the types. I do many of those things, but I also wanted a centralized place for users to change behavior, in the interest of model/view separation and easier unittesting in certain situations.
This led to theWebPresenter class. It converts normal D types, return values and exceptions, to html+http responses - a capability I have had for many years, but never customizable like this.
If you want to change the default, you define a child class using the CRTP technique and override functions. So far, I am liking it. More as it develops.
I also watched the DConf videos, which are on theDLF youtube channel. I don't have a lot to say yet though, I'll come back to it.