Thread overview | |||||
---|---|---|---|---|---|
|
October 07, 2020 If and isType with arrays | ||||
---|---|---|---|---|
| ||||
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 Re: If and isType with arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to DMon | 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 Re: If and isType with arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | 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.
|
Copyright © 1999-2021 by the D Language Foundation