Implementing Template Restrictions in Different Way

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 1 15:46:51 PDT 2014


Forgetting to do


import std.functional: binaryFun;


before trying to compile

bool skipOverSafe(alias pred = "a == b", R1, R2)(ref R1 r1, R2 r2)
     @safe pure if (is(typeof(binaryFun!pred(r1.front, r2.front))))
{
     return r1.length >= r2.length && skipOver!pred(r1, r2); // 
TODO: Can we prevent usage of .length?
}

unittest
{
     auto s1 = "Hello world";
     assert(!skipOverSafe(s1, "Ha"));
}


gives no clue about the missing import in the diagnostics.

Is this a other/newer preferred way to describe the template 
restriction, using for example __traits(compiles, ...)? Is

is(typeof(...

the fastest way to do this?

BTW: Is there a way to prevent the calls to r1.length and 
r2.length in this case?


More information about the Digitalmars-d-learn mailing list