If and isType with arrays

Paul Backus snarwin at gmail.com
Wed Oct 7 16:39:07 UTC 2020


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`.


More information about the Digitalmars-d-learn mailing list