Sequence separation

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 17 12:15:48 PDT 2016


On 08/17/2016 08:38 PM, Engine Machine wrote:
> On Wednesday, 17 August 2016 at 08:37:32 UTC, Lodovico Giaretta wrote:
[...]
>> You mean something like:
>>
>> struct MySequence(Args...)
>> {
>>     enum length = Args.length;
>>     alias args = Args;
>> }
>>
>> alias x = MySequence!(a, b, MySequence!(c, d));
>>
>> static assert(x.length == 3)
>> static assert(x.args[2].length == 2);
>
> Thanks, basically works.
>
> How can I test, though, if a argument uses a MySequence? I can't do if
> (Args[0] == MySequence) because MySequence is templated. While I could
> test for a length, that doesn't work because some types have a length. I
> could add another enum to MySequence, but again, not safe.
>
> I could do some string tests, but that doesn't work.
>
> in your exmaple,
>
> if (x.args[2] == MySequence) ??
>
> I simply need to differentiate between a parameter/arg being a
> MySequence and not.

With MySequence being a type, you can do this:

----
static if (is(x.args[2] == MySequence!Args, Args ...))
{
   ...
}
----

Aside from this check, there is probably not much use for MySequence 
being a type. So I'm be tempted to find a way to do the check with a raw 
template MySequence.

As you said, another enum alone doesn't cut it. The faker can just add 
the same enum.

But a private enum of a private type might do it:

----
template MySequence(Args ...)
{
     /* ... length and args ... */
     private enum id = Id();
}

private struct Id {}

enum isMySequence(alias seq) =  is(typeof(seq.id) == Id);
----

Other modules can't use the Id type directly, because it's private. And 
they can't use typeof(MySequence!foo.id), because the id member is 
private, too.

However, I wouldn't be surprised if this can be circumvented too.


More information about the Digitalmars-d-learn mailing list