const A = struct {
a: u8,
};
then:
_ = @TypeOf(A.a);
results in:
error: struct ‘main.A’ has no member named ‘a’
but it DOES have a member named a! I’m missing something simple, I’m sure.
const A = struct {
a: u8,
};
then:
_ = @TypeOf(A.a);
results in:
error: struct ‘main.A’ has no member named ‘a’
but it DOES have a member named a! I’m missing something simple, I’m sure.
I think you need to use @typeInfo
so that you can access the field types.
E.g. You could iterator through the type’s fields:
for (@typeInfo(A).@"struct".fields) |field| {
const field_type = field.type;
...
}
A is the struct type, which doesn’t have a .a
member, only an instance of type A has a .a
member (which is a field of that instance).
You can use:
@FieldType(A, "a")
I didn’t know about @FieldType
. Thanks!
It was added in 0.14.0
to make some code more performant and less awkward Proposal: add `@FieldType` · Issue #21702 · ziglang/zig · GitHub.
Ok, thank you both. I’ll concede that an INSTANCE of A would “rightly” have an A.a, but there is a simplicity to notion that @TypeOf(A.a)
should indeed give the type of the a field in the A struct, which is known at comptime and plainly declared. I wonder why it’s not a proper use of @TypeOf. I would prefer it to having to put “a” in quotes. Oh well.
The @TypeOf
evaluates the type of an expression. A.a
isn’t a valid expression. There is simplicity in that too.
Agreed. Incorrect simplicity is simply incorrect.
I might have guessed @FieldType to work as @FieldType(A.a) or something similar, but I guess it’s semantics and I DO agree to the need for an @FieldType, here, rather than an incorrect hijack of @TypeOf. Done.
I used to do @TypeOf(@as(A, undefined).a)
before @FieldType
was introduced.
Ah, clever! But only slightly less contrived than stringing out a typeInfo() construct. Good thing @FieldType came along, eh?
I thought it worth mentioning, for posterity, that sometimes a different pattern altogether is worth considering. It’s what I did, in this case. So, the use-case is that: elsewhere in my code, I need a variable that is going to be the same type as that struct field. If that’s likely at all, it’s worth considering:
pub const Magnitude = u64;
Then, inside of the struct, for the field, magnitude: Magnitude,
and anywhere else that a user may need that, likewise - import and use it.