www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Syntax of isExpression

reply Alex <sascha.orlov gmail.com> writes:
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
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
parent Alex <sascha.orlov gmail.com> writes:
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!
Jan 31 2017