Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
March 25, 2013 isExpression with bool template parameter | ||||
---|---|---|---|---|
| ||||
Trying to figure out how to pattern-match struct S below with an isExpression: struct S(A, bool B) {} static assert ( !is(S!(int, false) _ == S!(U, bool), U) ); static assert ( !is(S!(int, false) _ == S!(U, V), U, V) ); static assert ( !is(S!(int, false) _ == S!(U, V), U, V : bool) ); void main(){} Can someone help? |
March 26, 2013 Re: isExpression with bool template parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On 03/25/2013 04:57 PM, cal wrote:
>
> Trying to figure out how to pattern-match struct S below with an
> isExpression:
>
> struct S(A, bool B) {}
>
> static assert ( !is(S!(int, false) _ == S!(U, bool), U) );
> static assert ( !is(S!(int, false) _ == S!(U, V), U, V) );
> static assert ( !is(S!(int, false) _ == S!(U, V), U, V : bool) );
>
> void main(){}
>
> Can someone help?
I am not sure that I understand but usually the test is done in a context like foo() below:
struct S(A, bool B) {}
void foo(T)()
{
static assert(is(T == S!(U, false), U));
}
void main()
{
foo!(S!(int, false))();
}
Ali
|
March 26, 2013 Re: isExpression with bool template parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 26 March 2013 at 05:28:22 UTC, Ali Çehreli wrote:
> I am not sure that I understand but usually the test is done in a context like foo() below:
>
> struct S(A, bool B) {}
>
> void foo(T)()
> {
> static assert(is(T == S!(U, false), U));
> }
>
> void main()
> {
> foo!(S!(int, false))();
> }
>
> Ali
Maybe I gave a bad example, I guess I wonder why line A passes but line B fails:
struct S(A, bool B) {}
static assert(is(S!(int, false) _ == S!(U, false), U)); // line A, pass
static assert(is(S!(int, false) _ == S!(U, V), U, V)); // line B, fail
void main(){}
I.e., why I can't match on some generic second parameter - what if the second parameter was an int:
struct S(A, int B) {}
static assert(is(S!(int, 5627) _ == S!(U, ??), U, ??));
how to match that in the isExpression?
cheers,
cal
|
March 26, 2013 Re: isExpression with bool template parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On Tuesday, 26 March 2013 at 06:03:55 UTC, cal wrote:
> I.e., why I can't match on some generic second parameter - what if the second parameter was an int:
>
> struct S(A, int B) {}
> static assert(is(S!(int, 5627) _ == S!(U, ??), U, ??));
>
> how to match that in the isExpression?
>
> cheers,
> cal
However this does work (and solves my problem):
struct S(A, int B){}
static if (is(S!(int, 5) _ : S!(U), U...)) // line B, fail
pragma(msg, U.stringof); // tuple(int, 5);
But it seems like is(S!(int, 5) _ : S!(U, V), U, V) should work to me.
|
March 26, 2013 Re: isExpression with bool template parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On Tuesday, 26 March 2013 at 06:15:54 UTC, cal wrote:
> static if (is(S!(int, 5) _ : S!(U), U...)) // line B, fail
> pragma(msg, U.stringof); // tuple(int, 5);
oops the '// line B, fail' should not be there
|
March 26, 2013 Re: isExpression with bool template parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On 03/25/2013 11:15 PM, cal wrote: > On Tuesday, 26 March 2013 at 06:03:55 UTC, cal wrote: >> I.e., why I can't match on some generic second parameter - what if the >> second parameter was an int: >> >> struct S(A, int B) {} >> static assert(is(S!(int, 5627) _ == S!(U, ??), U, ??)); >> >> how to match that in the isExpression? > But it seems like is(S!(int, 5) _ : S!(U, V), U, V) should work to me. There are three kinds of template parameters: type, value, and alias. That syntax does not work because you are trying to match 5 to V, where 5 is a value and V is a type. Ali |
March 26, 2013 Re: isExpression with bool template parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 26 March 2013 at 13:45:52 UTC, Ali Çehreli wrote:
> There are three kinds of template parameters: type, value, and alias.
>
> That syntax does not work because you are trying to match 5 to V, where 5 is a value and V is a type.
>
> Ali
Thanks Ali,
I guess the tuple version is fine, it is just surprising that only one of the three parameter types can be directly matched. It is also surprising that this example from the docs works:
alias Tuple!(int, string) Tup;
static if (is(Tup : TX!TL, alias TX, TL...))
(matching an alias) but the alias can't be directly matched when it appears as a parameter of the type.
|
March 28, 2013 Re: isExpression with bool template parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On Tuesday, 26 March 2013 at 14:26:59 UTC, cal wrote: > I guess the tuple version is fine, it is just surprising that only one of the three parameter types can be directly matched. It is also surprising that this example from the docs works: Because 'Tuple' is a template, not a type. Please recall that: struct Tuple(T...) { ... } is a syntactic sugar of: template Tuple(T...) { struct Tuple { ... } } > alias Tuple!(int, string) Tup; > static if (is(Tup : TX!TL, alias TX, TL...)) > > (matching an alias) but the alias can't be directly matched when it appears as a parameter of the type. So, `alias TX` will bind the template Tuple. There is no problem. Kenji Hara |
Copyright © 1999-2021 by the D Language Foundation