digitalmars.D.learn - Syntax of isExpression
- Alex (31/31) Jan 31 2017 Hey guys,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (8/16) Jan 31 2017 That's because 'true' is not the type 'bool' but a value of bool. You
- Alex (3/19) Jan 31 2017 Ah...
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.
Jan 31 2017
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
Jan 31 2017
On Tuesday, 31 January 2017 at 11:40:06 UTC, Ali Çehreli wrote:On 01/31/2017 02:49 AM, Alex wrote:Ah... Thanks!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
Jan 31 2017