Sanity check: pithiest test for static if: "is an array of ubyte"
Nick Treleaven
nick at geany.org
Sat May 24 21:02:10 UTC 2025
On Saturday, 24 May 2025 at 20:29:52 UTC, Andy Valencia wrote:
> The best I"ve come up with for a "static if" to see if
> something's an array of ubyte:
OK, so e.g. `ubyte[] arg`.
> ```d
> (isArray!(typeof(arg))) &&
> ```
That's fine.
> is(typeof(arg).init[0] == ubyte)
1. Unfortunately `typeof(arg).init` gives `null` when `arg` is a
dynamic array, so indexing won't work. Note: intuitively it would
be `[]`, though perhaps with the same type as `arg`, but it isn't.
However, you already have a value - `arg`. So you can just use
`arg[0]`.
2. `typeof(arg).init[0]` is a value, not a type. `is()` (almost)
always takes a type for its first parameter. So for a static
array `arg` only, `is(typeof(arg.init[0]) == ubyte)` does work.
Fixing both, you can use `is(typeof(arg[0]) == ubyte)`.
More information about the Digitalmars-d-learn
mailing list