Thread overview
"is" expression and type tuples
Mar 03, 2015
Jack Applegame
Mar 03, 2015
bearophile
Mar 03, 2015
Jack Applegame
Mar 03, 2015
bearophile
Mar 04, 2015
Jack Applegame
Mar 04, 2015
bearophile
March 03, 2015
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?
March 03, 2015
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
March 03, 2015
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..$]);
> }
March 03, 2015
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
March 04, 2015
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?
March 04, 2015
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