Template-Parameterized Variadic isInstaceOf

Tofu Ninja via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 7 16:08:22 PDT 2015


On Friday, 7 August 2015 at 14:45:44 UTC, Nordlöw wrote:
> On Friday, 7 August 2015 at 14:30:55 UTC, Nordlöw wrote:
>> Any suggestions on adding support for `binaryFun!pred` aswell?
>
> I cracked it.
>
> template isSortedRange(T, alias pred = "a < b")
> {
>     import std.traits : TemplateArgsOf;
>
>     static if (TemplateArgsOf!T.length == 2)
>     {
>         import std.functional : binaryFun;
>
>         alias predArg = TemplateArgsOf!T[1];
>         static if (isSomeString!(typeof(pred)))
>         {
>             alias predFun = binaryFun!pred;
>         }
>         else
>         {
>             alias predFun = pred;
>         }
>
>         static if (isSomeString!(typeof(predArg)))
>         {
>             alias predArgFun = binaryFun!predArg;
>         }
>         else
>         {
>             alias predArgFun = predArg;
>         }
>
>         enum isSortedRange = (is(T == SortedRange!Args, 
> Args...) &&
>                               is(typeof(predFun) == 
> typeof(predArgFun)));
>     }
>     else
>     {
>         enum isSortedRange = false;
>     }
> }
>
> ///
> unittest
> {
>     import std.functional : binaryFun;
>
>     alias R = int[];
>     enum pred = "a < b";
>
>     alias SR = SortedRange!(R, pred);
>     static assert(isSortedRange!(SR, pred));
>     static assert(isSortedRange!(SR, binaryFun!pred));
>
>     alias SR2 = SortedRange!(R, binaryFun!pred);
>     static assert(isSortedRange!(SR2, pred));
>     static assert(isSortedRange!(SR2, binaryFun!pred));
> }
>
> Comments, please.

I think you could have omitted the need for a predicate with just 
using isInstanceOf.

enum isSortedRange(T) = isInstanceOf!(SortedRange, T);

But I think yours works better for what you are trying to do, the 
pred of whatever function you are trying to specialize needs to 
match the pred of the sorted range itself, else the 
specialization is wrong.



More information about the Digitalmars-d-learn mailing list