Fallback 'catch-all' template functions

Meta via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 1 10:21:02 PDT 2016


On Thursday, 1 September 2016 at 16:50:49 UTC, Steven 
Schveighoffer wrote:
> I agree. Note that if(isSomethingElse!T) may also need to have 
> if(!isSomething!T && isSomethingElse!T).
>
> A suggestion in the past was to allow "else" clauses with if 
> constraints. I had envisioned:
>
> void f(T)(T t) if(isSomething!T) {}
> void f(T)(T t) else if(isSomethingElse!T) {}
> void f(T)(T t) else {}
>
> But someone also suggested this more DRY solution as well 
> (can't remember the thread for it):
>
> void f(T)(T t) if(isSomething!T) {}
>           else if(isSomeghingElse!T) {}
>           else {}
>
> Obviously this doesn't work across modules, but how does that 
> even work? You need some sort of ordering for an if/else 
> if/else scheme to work.
>
> Having a "fallback" template could potentially define a way to 
> handle the default, but it doesn't fix the other issues.
>
> I don't know if it's because of the current rules, or just 
> natural, but typically I'm not splitting my template functions 
> between many modules.
>
> -Steve

I just thought of this, but cannot test if it works. If it does, 
maybe it would be a suitable solution?

void f(T)(T t) if(isSomething!T) {}
void f(T)(T t) if(isSomethingElse!T) {}
//Taken if no other "overload" of f will intantiate with the 
given T
void f(T)(T t) if(!__traits(compiles, alias _ = .f!T)) {}


More information about the Digitalmars-d mailing list