@FieldType() differs semantically from @TypeOf(@field()) #35323

Open
opened 2026-05-16 03:35:28 +02:00 by anordinaryuser · 2 comments

Zig Version

0.16.0

Steps to Reproduce and Observed Behavior

  1. Use any struct, T, with a method named "method"
  2. Call @TypeOf(@field(T, "method")). This returns a type representing the function signature of "method". This pattern works for all types of members, including both fields and methods.
  3. Call @FieldType(T, "method"). This will cause an error because @FieldType only works for fields, not methods.

Expected Behavior

The names of @FieldType and @TypeOf(@field()) imply they are logically equivalent. This relates somewhat to #32029 (changing @field to @member), which would fix the issue of these reading as effectively the same despite being different. However, this would also leave a gap for something like an @MemberType builtin to be used instead of requiring two builtins.

### Zig Version 0.16.0 ### Steps to Reproduce and Observed Behavior 1. Use any struct, T, with a method named "method" 2. Call @TypeOf(@field(T, "method")). This returns a type representing the function signature of "method". This pattern works for all types of members, including both fields and methods. 3. Call @FieldType(T, "method"). This will cause an error because @FieldType only works for fields, not methods. ### Expected Behavior The names of @FieldType and @TypeOf(@field()) imply they are logically equivalent. This relates somewhat to #32029 (changing @field to @member), which would fix the issue of these reading as effectively the same despite being different. However, this would also leave a gap for something like an @MemberType builtin to be used instead of requiring two builtins.
Contributor

yep, the reason I wrote #32029 (comment)

yep, the reason I wrote https://codeberg.org/ziglang/zig/issues/32029#issuecomment-13920188

@nektro wrote in #32029 (comment):

@FieldType -> @Member should be included in this

Suppose a type T that have field a x, and declaration a c, and suppose b an instance of T.
@FieldType exist because even thought you can get the type of a field by @TypeOf(b.x) and this is resolved at comptime, it's not possible to do so without creating an instance of T first. Another method would involve getting the @typeInfo of T then the type of x from it.
Where as declarations can be referenced directly once you have the type/namespace -> @TypeOf(T.c). While the @field/@member is still useful in both cases (due to the ability to programmatically generate or inspect types at comptime), it's not the same for @FieldType. @TypeOf(T.c) or @TypeOf(@member(T, "c")) can be directly used for declarations, for fields you can ONLY use @FieldType (unless you have on hand an INSTANCE of T, or resolve to @TypeOf(@as(T, undefined).x) which was considered an "ugly hack" f83bb94c).
Further more, this distinction already exists in other buitins such as @hasField and @hasDecl, the later only consider public declarations. As of 0.17 even private declaration from the same file are not considered by @hasDecl while T.c and @field(T, "c") only fail for different files; this is in itself somewhat inconsistent but--- that is even the case for struct info's decl_names. It's not clear if @FieldType or @MemberType when supporting declarations too, should also "error: no field c in T" if c where to be private.

In short, because of their differences (in nature and usages), and also because of the ability to directly T.c or @member(T, "c"), I think, there is no need for a @FieldType or @MemberType that would need to work on both fields and declarations. The rationals for the existence of @FieldType in itself was 1: avoid the mentioned ugly hack, 2: avoid performance implications of a @typeInfo based approach. Thus #35323 alone should fix this issues.

@nektro wrote in https://codeberg.org/ziglang/zig/issues/32029#issuecomment-13920188: > `@FieldType` -> `@Member` should be included in this Suppose a type `T` that have field a `x`, and declaration a `c`, and suppose `b` an instance of `T`. `@FieldType` exist because even thought you can get the type of a field by `@TypeOf(b.x)` and this is resolved at comptime, it's not possible to do so without creating an instance of `T` first. Another method would involve getting the `@typeInfo` of `T` then the type of `x` from it. Where as declarations can be referenced directly once you have the type/namespace -> `@TypeOf(T.c)`. While the `@field`/`@member` is still useful in both cases (due to the ability to programmatically generate or inspect types at comptime), it's not the same for `@FieldType`. `@TypeOf(T.c)` or `@TypeOf(@member(T, "c"))` can be directly used for declarations, for fields you can ONLY use `@FieldType` (unless you have on hand an INSTANCE of T, or resolve to `@TypeOf(@as(T, undefined).x)` which was considered an "ugly hack" f83bb94c). Further more, this distinction already exists in other buitins such as `@hasField` and `@hasDecl`, the later only consider public declarations. As of 0.17 even private declaration from the same file are not considered by `@hasDecl` while `T.c` and `@field(T, "c")` only fail for different files; this is in itself somewhat inconsistent but--- that is even the case for struct info's `decl_names`. It's not clear if `@FieldType` or `@MemberType` when supporting declarations too, should also "error: no field `c` in `T`" if `c` where to be private. In short, because of their differences (in nature and usages), and also because of the ability to directly `T.c` or `@member(T, "c")`, I think, there is no need for a `@FieldType` or `@MemberType` that would need to work on both fields and declarations. The rationals for the existence of `@FieldType` in itself was 1: avoid the mentioned ugly hack, 2: avoid performance implications of a `@typeInfo` based approach. Thus https://codeberg.org/ziglang/zig/issues/35323 alone should fix this issues.
Sign in to join this conversation.
No milestone
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ziglang/zig#35323
No description provided.