September 01, 2016
On 9/1/16, Manu via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> So, consider a set of overloads:
>
>   void f(T)(T t) if(isSomething!T) {}
>   void f(T)(T t) if(isSomethingElse!T) {}
>   void f(T)(T t) {}
>

Best thing I can think of is to use a forwarding template:

-----
import std.stdio;

// forwarding (or dispatching) template
void f(T)(T t)
{
    static if(is(typeof( fImpl(t) )))  // found match
    {
        fImpl(t);
    }
    else  // no matches, use fallback
    {
        fallback(t);
    }
}

void fImpl(T)(T t) if (is(T == int)) { writefln("Int: %s", t); }
void fImpl(T)(T t) if (is(T == float)) { writefln("Float: %s", t); }
void fallback(T)(T t) { writefln("Generic: %s", t); }

void main ( )
{
    f(int(1));
    f(float(1.0));
    f("string");
}
-----
September 01, 2016
On Thu, Sep 01, 2016 at 03:37:50PM +1000, Manu via Digitalmars-d wrote:
> So, consider a set of overloads:
> 
>   void f(T)(T t) if(isSomething!T) {}
>   void f(T)(T t) if(isSomethingElse!T) {}
>   void f(T)(T t) {}
> 
> I have a recurring problem where I need a fallback function like the bottom one, which should be used in lieu of a more precise match. This is obviously an ambiguous call, but this is a pattern that comes up an awful lot. How to do it in D?
> 
> I've asked this before, and people say:
> 
>   void f(T)(T t) if(!isSomething!T && !isSomethingElse!T) {}
> 
> Consider that more overloads are being introduced by users spread out across many modules that define their own kind of T; this solution is no good.

Here's a possible workaround:

	void f(T)(T t)
	{
		static if (isSomething!T) { ... }
		else static if (isSomethingElse!T) { ... }
		else { ... }
	}

But this won't work across modules. :-(


T

-- 
The early bird gets the worm. Moral: ewww...