Thread overview
Sanity check: pithiest test for static if: "is an array of ubyte"
5 days ago
Andy Valencia
5 days ago
Nick Treleaven
5 days ago
H. S. Teoh
5 days ago
Andy Valencia
5 days ago
Andy Valencia
5 days ago

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.

TIA!
Andy

5 days ago

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.

>
(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].

  1. 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).

5 days ago

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:

is(typeof(arg) == ubyte[])

5 days ago
On Sat, May 24, 2025 at 08:29:52PM +0000, Andy Valencia via Digitalmars-d-learn wrote:
> The best I"ve come up with for a "static if" to see if something's an array of ubyte:
> 
> ```d
> (isArray!(typeof(arg))) && is(typeof(arg).init[0] == ubyte)
> ```

That's totally overkill.  Just do this:

	T[] myArray;
	static if (is(typeof(myArray) == ubyte[])) {
		...
	}

That's all.  Dead simple, no need to bring out the cannons when a screwdriver will suffice.


--T
5 days ago
On Saturday, 24 May 2025 at 21:58:16 UTC, H. S. Teoh wrote:
> 	static if (is(typeof(myArray) == ubyte[])) {
> 		...
> 	}

Oh!  I knew I was missing something.  Thank you to both of you for pointing at this.

Andy

5 days ago

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

5 days ago

On Sunday, 25 May 2025 at 01:06:12 UTC, Steven Schveighoffer wrote:

>

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.

Actually, I was a little bit curious about the case where my arg was zero length... that addresses it nicely. Thank you.

Andy