Fallback 'catch-all' template functions

Manu via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 1 02:08:31 PDT 2016


On 1 September 2016 at 18:44, Stefan Koch via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> On Thursday, 1 September 2016 at 05:37:50 UTC, Manu 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.
>
>
> To my knowledge there is currently no clean way of doing this.
> The easiest workaround would be to introduce another name for the
> implementation.
>
> then it would look like
> void f(T)(T t) {
>   static if (is(fImpl(t) == void)) {
>     f(t);
>   } else {
>     // default impl here
>   }
> }

This was my fallback plan, but it seems a bit shit.


More information about the Digitalmars-d mailing list