www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "is" expression and type tuples

reply "Jack Applegame" <japplegame gmail.com> writes:
Seems like "is" expression doesn't support type tuples:

 pragma(msg, is(short : int));                 // true

 enum Test(ARGS...) = is(ARGS[0..2] : ARGS[2..4]);
 pragma(msg, is(Test!(int, int, int, int)));   // false
 pragma(msg, Test!(int, short, int, int));     // false
Is it by design, or just not implemented?
Mar 03 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Jack Applegame:

 Seems like "is" expression doesn't support type tuples:

 pragma(msg, is(short : int));                 // true

 enum Test(ARGS...) = is(ARGS[0..2] : ARGS[2..4]);
 pragma(msg, is(Test!(int, int, int, int)));   // false
 pragma(msg, Test!(int, short, int, int));     // false
Is it by design, or just not implemented?
It's by design, perhaps because Walter didn't think of this case, or probably for compiler simplicity. But it should be not too much hard to implement it your code. Just use two is(), or use recursion (with splitting in two, and not 1 + n-1). Bye, bearophile
Mar 03 2015
parent reply "Jack Applegame" <japplegame gmail.com> writes:
On Tuesday, 3 March 2015 at 16:42:22 UTC, bearophile wrote:
 But it should be not too much hard to implement it your code. 
 Just use two is(), or use recursion (with splitting in two, and 
 not 1 + n-1).

 Bye,
 bearophile
I already have one:
 template Is(ARGS...) if(ARGS.length % 2 == 0) {
     enum N = ARGS.length/2;
     static if(N == 1) enum Is = is(ARGS[0] : ARGS[1]);
     else enum Is = is(ARGS[0] : ARGS[N]) && Is!(ARGS[1..N], 
 ARGS[N+1..$]);
 }
Mar 03 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Jack Applegame:

 or use recursion (with splitting in two, and not 1 + n-1).

 Bye,
 bearophile
I already have one:
 template Is(ARGS...) if(ARGS.length % 2 == 0) {
    enum N = ARGS.length/2;
    static if(N == 1) enum Is = is(ARGS[0] : ARGS[1]);
    else enum Is = is(ARGS[0] : ARGS[N]) && Is!(ARGS[1..N], 
 ARGS[N+1..$]);
 }
That's 1 + n-1 :-) Bye, bearophile
Mar 03 2015
parent reply "Jack Applegame" <japplegame gmail.com> writes:
On Tuesday, 3 March 2015 at 17:49:24 UTC, bearophile wrote:
 That's 1 + n-1 :-)
Could you please explain what does '1 + n-1' mean?
Mar 03 2015
parent "bearophile" <bearophileHUGS lycos.com> writes:
Jack Applegame:

 On Tuesday, 3 March 2015 at 17:49:24 UTC, bearophile wrote:
 That's 1 + n-1 :-)
Could you please explain what does '1 + n-1' mean?
This is your code:
 template Is(ARGS...) if(ARGS.length % 2 == 0) {
    enum N = ARGS.length/2;
    static if(N == 1) enum Is = is(ARGS[0] : ARGS[1]);
    else enum Is = is(ARGS[0] : ARGS[N]) && Is!(ARGS[1..N], 
 ARGS[N+1..$]);
 }
The recursion scheme you are using is working on a single item (a single pair of items), and then calling the recursion on all other items but the first. If you look in std.traits you see examples of a different recursion that reduces compilation time: template isExpressionTuple(T ...) { static if (T.length >= 2) enum bool isExpressionTuple = isExpressionTuple!(T[0 .. $/2]) && isExpressionTuple!(T[$/2 .. $]); else static if (T.length == 1) enum bool isExpressionTuple = !is(T[0]) && __traits(compiles, { auto ex = T[0]; }); else enum bool isExpressionTuple = true; // default } Bye, bearophile
Mar 04 2015