Thread overview | |||||
---|---|---|---|---|---|
|
January 31, 2017 Syntax of isExpression | ||||
---|---|---|---|---|
| ||||
Hey guys, could you help me understand the syntax of the isExpression? I have an example, leaned on documentation https://dlang.org/spec/expression.html#IsExpression case 7. // Code starts here // import std.stdio, std.typecons; alias Tup = Tuple!(int, string, bool); enum myType {a, b, c} struct E(T, myType ET, bool P) { T parts; } void main() { auto r = E!(int, myType.a, true)(); fun(Tup.init); writeln("-----"); fun(r); } void fun(T)(T t) { static if (is(T : TX!TL, alias TX, TL...)) { writeln(is(TL[0] == int)); writeln(typeid(TL[1])); writeln(typeid(TL[2])); writeln(is(TL[2] == bool)); } } // Code ends here // The question is: why during testing with Tup the last line gives "true" and with the struct it is false? Especially, while the line before is the same for both cases. |
January 31, 2017 Re: Syntax of isExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On 01/31/2017 02:49 AM, Alex wrote: > auto r = E!(int, myType.a, true)(); > static if (is(T : TX!TL, alias TX, TL...)) > { > writeln(is(TL[0] == int)); > writeln(typeid(TL[1])); > writeln(typeid(TL[2])); > writeln(is(TL[2] == bool)); > } That's because 'true' is not the type 'bool' but a value of bool. You can ask whether the type of a value is bool: writeln(is(typeof(TL[2]) == bool)); But then it fails for the Tuple case because typeof(bool) is not bool in that case. You need to cover all cases if both 'bool' and 'true' mean the same thing for you. Ali |
January 31, 2017 Re: Syntax of isExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 31 January 2017 at 11:40:06 UTC, Ali Çehreli wrote:
> On 01/31/2017 02:49 AM, Alex wrote:
>
> > auto r = E!(int, myType.a, true)();
>
> > static if (is(T : TX!TL, alias TX, TL...))
> > {
> > writeln(is(TL[0] == int));
> > writeln(typeid(TL[1]));
> > writeln(typeid(TL[2]));
> > writeln(is(TL[2] == bool));
> > }
>
> That's because 'true' is not the type 'bool' but a value of bool. You can ask whether the type of a value is bool:
>
> writeln(is(typeof(TL[2]) == bool));
>
> But then it fails for the Tuple case because typeof(bool) is not bool in that case. You need to cover all cases if both 'bool' and 'true' mean the same thing for you.
>
> Ali
Ah...
Thanks!
|
Copyright © 1999-2021 by the D Language Foundation