On Tuesday, 24 March 2015 at 16:56:13 UTC, matovitch wrote:
Thanks, just to be clear :

void Bar(T : Foo)(T t){
}

is the same as

void Bar(T)(T t) if (is(T == Foo)){
}

and it is checked only at compile time ? (for the runtime I know that what interface were meant for ;)).

Ali already mentioned the difference between "==" and ":".

In addition to that, template specializations (like `Bar(T : Foo)(T t)`) and template constraints (like `Bar(T)(T t) if(is(T : Foo))`) are similar but generally not interchangeable.

A template with a specialization is considered a better match than one without. Whereas a template constraint doesn't add to the quality of the match.

An example:

module test;
import std.stdio;

void f(T)(T t) {writeln("generic");}
void f(T : int)(T t) {writeln("with specialization");}
void f(T)(T t) if(is(T : Object)) {writeln("with constraint");}

void main()
{
    f("some string"); /* -> "generic" */
    f(42); /* -> "with specialization" */
version(none) f(new Object); /* Doesn't compile, because it matches both the generic version and the one with the constraint. */
}

Reply via email to