Compiletime select type for bitwise length

I have this constant defined:
pub const WIDTH: usize = 15;

Now I would like to create an alias for a type which is the bit-length of WIDTH.
I believe I saw that somewhere as a @builtin function, but cannot find it anymore.

Something like this:
pub const MyWidthType = @selectType(WIDTH)

which - in this case - should map to u15.

You can use an non exhaustive enum to create a new type:

pub const MyWidthType = enum(u15) { _ };

If you want to return the type u15 from the value 15 you can use @Type or the helper std.meta.Int:

pub const MyWidthType = enum(std.meta.Int(.unsigned, 15)) { _ };
5 Likes

Thanks! @Type it was.
I should study that enum as well…