www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template-Parameterized Variadic isInstaceOf

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
To implement a new trait

     isSortedRange(R, pred)

needed for SortedRange specializations I need a variant of

     enum bool isInstanceOf(alias S, T) = is(T == S!Args, Args...);

that takes the `pred` argument aswell.

But I have no clue what to do with

     enum bool isInstanceOf(alias S, T, TParams) = is(T == S!Args, 
Args...);

because I dont' understand the syntax

     is(T == S!Args, Args...);

Can somebody please explain and help out with variadic version of 
`isInstanceOf`?
Aug 07 2015
next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote:

     enum bool isInstanceOf(alias S, T, TParams)
Correction: enum bool isInstanceOf(alias S, T, TParams...)
Aug 07 2015
prev sibling next sibling parent "Nicholas Wilson" <iamthewilsonator hotmail.com> writes:
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote:
 To implement a new trait

     isSortedRange(R, pred)

 needed for SortedRange specializations I need a variant of

     enum bool isInstanceOf(alias S, T) = is(T == S!Args, 
 Args...);

 that takes the `pred` argument aswell.

 But I have no clue what to do with

     enum bool isInstanceOf(alias S, T, TParams) = is(T == 
 S!Args, Args...);

 because I dont' understand the syntax

     is(T == S!Args, Args...);

 Can somebody please explain and help out with variadic version 
 of `isInstanceOf`?
I believe that it is read as "T is equal to the template instansiation of S for some args Args... Similar to the more common (T ==V[K],V,K) read as " an associative array with V as the value, K as the key for some V and some K" Used to get the instansiation parameters.
Aug 07 2015
prev sibling next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote:
 Can somebody please explain and help out with variadic version 
 of `isInstanceOf`?
Here's a step forward: /** Returns true if $(D T) is an instance of the template $(D T) with template parameters $(D Ps). */ enum bool isInstanceOf(alias S, T, Ps...) = is(T == S!Ps); /// unittest { struct SortedRange(Range, alias pred = "a < b") { } alias R = int[]; alias SR = SortedRange!(R, "a < b"); static assert(isInstanceOf!(SortedRange, SR, R, "a < b")); } 1. This, however, requires *all* the template parameters to be given. What I need now is a syntax to check that, in this case, *only* the second template argument `pred` matches. How do I do that? 2. Note also that this solution doesn't understand that "a<b" and "a < b" are semantically equivalent. Forcing usage of `binaryFun` is a temporary solution. Is there a better solution?
Aug 07 2015
prev sibling next sibling parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote:
 Can somebody please explain and help out with variadic version 
 of `isInstanceOf`?
Here's a try at isSortedRange: enum bool isSortedRange(T, alias pred = "a < b") = is(T == SortedRange!(Args[0], pred), Args...); unittest { alias R = int[]; enum pred = "a < b"; static assert(isSortedRange!(SortedRange!(R, pred), pred)); } but it fails. This simplified case enum bool isSortedRange(T, alias pred = "a < b") = is(T == SortedRange!Args, Args...); works. How do check that the second template argument to the instance of SortedRange matches `pred`?
Aug 07 2015
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Friday, 7 August 2015 at 14:13:24 UTC, Nordlöw wrote:
 How do check that the second template argument to the instance 
 of SortedRange matches `pred`?
Using TemplateArgsOf. I found a solution: template isSortedRange(T, alias pred = "a < b") { import std.traits : TemplateArgsOf; enum isSortedRange = (is(T == SortedRange!Args, Args...) && pred == (TemplateArgsOf!T[1])); } /// unittest { alias R = int[]; enum pred = "a < b"; alias SR = SortedRange!(R, pred); static assert(isSortedRange!(SR, pred)); } Any suggestions on adding support for `binaryFun!pred` aswell? My try enum isSortedRange = (is(T == SortedRange!Args, Args...) && is(binaryFun!pred == binaryFun!(TemplateArgsOf!T[1]))); fails.
Aug 07 2015
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
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.
Aug 07 2015
parent "Tofu Ninja" <emmons0 purdue.edu> writes:
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.
Aug 07 2015
prev sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote:
 To implement a new trait

     isSortedRange(R, pred)

 needed for SortedRange specializations I need a variant of
I've put all my progress into https://github.com/D-Programming-Language/phobos/pull/3534
Aug 08 2015