Search code examples
d

Dlang : how to enforce an interface on a template function


I've been googling for two hours looking for this with no luck.

If I have a template function and I want to enforce an interface on the template type, how do I do so?

ex.

void doStuff(T)(bool param) /*if T is a Throwable*/ {
    // do stuff...
}

Solution

  • You can do this using a template constraint:

    void doStuff(T)(bool param) if(is(T : Throwable)) { }
    

    is expressions can be quite complex, so I suggest having a read through this.