#Getting array-length of a struct field at comptime

1 messages · Page 1 of 1 (latest)

cobalt cloak
#

i feel like there's an easy way to do this that i'm totally missing right now:

const Foo = struct {
    array: [5]u8,
};

comptime {
    @compileLog(Foo.array.len);
}

errors with:

error: struct 'Foo' has no member named 'array'
    @compileLog(Foo.array.len);
                ~~~^~~~~~

I can get it working with

@typeInfo(std.meta.FieldType(Foo, .array)).Array.len

but i feel like there's probably a simple way to go about this

sullen ember
#

array isnt static?

cobalt cloak
#

right, it's a field

sullen ember
#

const array: [5]u8 = undefined

fleet solstice
#

im pretty sure this was talked about recently

sullen ember
#

you'd need to make it static to access the len 🤔

cobalt cloak
sullen ember
#

no, i understand you can do it via reflection

#

sorry i mean, there isnt anything simpler no

#

currently, you'd need to make it static to access the len

crystal dust
#

how about
@as(Foo, undefined).array.len

cobalt cloak
sullen ember
#

very interesting

#

would be creating an anon Foo ...