digitalmars.D.learn - If and isType with arrays
- DMon (17/17) Oct 07 2020 Can isType be used as a condition in an if statement with arrays?
- Paul Backus (10/28) Oct 07 2020 You can do this with `is()` and `typeof()`:
- DMon (5/14) Oct 07 2020 I had previously gotten your example to work along with other
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
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
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: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.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:
Oct 07 2020