Thread overview
If and isType with arrays
Oct 07, 2020
DMon
Oct 07, 2020
Paul Backus
Oct 07, 2020
DMon
October 07, 2020
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");
    }
}
October 07, 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`.
October 07, 2020
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.