www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - If and isType with arrays

reply DMon <no mail.com> writes:
Can isType be used as a condition in an if statement with arrays?

import std.stdio;
import std.traits;

void main()
{
     int[5] a = [1,2,3,4,5];

// Something like this:
     if (a == isType!(int[5]))
     {
         write("true");
     }

// This works:
     if (a[0] == isType!(int))
     {
         write("true");
     }
}
Oct 07 2020
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 7 October 2020 at 16:25:33 UTC, DMon wrote:
 Can isType be used as a condition in an if statement with 
 arrays?

 import std.stdio;
 import std.traits;

 void main()
 {
     int[5] a = [1,2,3,4,5];

 // Something like this:
     if (a == isType!(int[5]))
     {
         write("true");
     }

 // This works:
     if (a[0] == isType!(int))
     {
         write("true");
     }
 }
You can do this with `is()` and `typeof()`: if (is(typeof(a) == int[5])) { write("true"); } The example you have that "works" is just a coincidence: `isType!(int)` evaluates to the boolean value `true` (because `int` is, indeed, a type), which compares equal to the integer `1`.
Oct 07 2020
parent DMon <no mail.com> writes:
On Wednesday, 7 October 2020 at 16:39:07 UTC, Paul Backus wrote:
 On Wednesday, 7 October 2020 at 16:25:33 UTC, DMon wrote:
 Can isType be used as a condition in an if statement with 
 arrays?
You can do this with `is()` and `typeof()`: if (is(typeof(a) == int[5])) { write("true"); } The example you have that "works" is just a coincidence:
I had previously gotten your example to work along with other basic properties to enter the if statement and isType in the write function. Thanks for the clarification and the note on the second part.
Oct 07 2020