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:
(isArray!(typeof(arg))) && is(typeof(arg).init[0] == ubyte)
Which doesn't seem to work, but it's the closest I've come by poring over the docs and reading Phobos source.
Aside from the better options suggested by others, I can explain why what you wrote didn't work.
the is
expression requires a type. Instead, you passed it a value.
typeof(arg).init[0]
\_________/ \__/\_/
type | |
expression |
expression
The .init
property of a type is a value (the default value of that type), and then you indexed that value, which is empty, and of course, this does not correctly resolve at compile time.
What you need is to get the type of the entire expression.
In this case, you have to use typeof(...)
to get the type of an expression. It is important to note that an expression need not be executed to get its type. The compiler gets the type from analyzing the expression. So there actually is no need to convert arg
into a type and use init
, you really just need to get the type of the expression arg[0]
. So therefore:
static if(isArray!(typeof(arg))) && is(typeof(arg[0]) == ubyte))
Note that even if arg.length is 0, this is fine, because the expression isn't actually executed.
Hope this helps you to understand the issue.
-Steve