CRTP thoughts, named arguments DIP review, DConf videos now on youtube

Posted 2019-06-10

My thoughts on the potential of the curiously recurring template pattern in D

Core D Development Statistics

In the community

Community announcements

See more at the announce forum.

Tip of the Week

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:

1 class Base {
2     /* virtual */ void process(T)(T t) { /* default behavior */ }
3 }
4 
5 struct MyData {}
6 
7 class Child : Base {
8     // not gonna work :(
9     override void process(T : MyData)(T t) { }
10 }

With static 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:

1 mixin template DefaultProcessors() {
2     void process(T) {}
3 }
4 
5 class Child : Base {
6     mixin DefaultProcessors dp;
7     void process(T : MyData)(T t) {}
8     void process(T)(T t) { dp.process(t); } // needed for overload set resolution
9 }

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?

class Base(CRTP) {}
class Child : 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.

1 class Base(CRTP) {
2     void process(T)(T t) { import std.stdio; writeln("default process ", T.stringof); }
3     void process(T : int)(T t) { import std.stdio; writeln("specialized base int process"); }
4     void someTraditionalMethod() {
5          (cast(CRTP) this).process("my string");
6          (cast(CRTP) this).process(0);
7          (cast(CRTP) this).process([1]);
8     }
9 }
10 
11 class Child : Base!Child {
12     void process(T : string)(T t) {
13         import std.stdio; writeln("custom string process!");
14     }
15     void process(T)(T t) { super.process(t); } // still necessary to remind the compiler we do still want default fallback processing
16 }
17 
18 void main() {
19    auto child = new Child();
20    child.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 the override 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 inside Base 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 with static 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 cast this 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 classes final. 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.

What Adam is working on

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 the WebPresenter 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.

DConf 2019

I also watched the DConf videos, which are on the DLF youtube channel. I don't have a lot to say yet though, I'll come back to it.