rename @field to @member for clarity #32029

Open
opened 2026-04-22 21:04:54 +02:00 by andrewrk · 10 comments
Owner

Migrated (and mutated) from https://github.com/ziglang/zig/issues/3839

  • "fields" in zig have to do with struct fields for example.
  • "declarations" is our name for global stuff that is not fields.
  • "member" has some precedence for being either one or the other

@field is a.b syntax, it's not specifically for fields. For consistency, rename the builtin to @member.

Also for consistency, rename our "field access syntax" grammar to "member access syntax".

Migrated (and mutated) from https://github.com/ziglang/zig/issues/3839 * "fields" in zig have to do with struct fields for example. * "declarations" is our name for global stuff that is not fields. * "member" has some precedence for being either one or the other `@field` is `a.b` syntax, it's not specifically for fields. For consistency, rename the builtin to `@member`. Also for consistency, rename our "field access syntax" grammar to "member access syntax".
andrewrk added this to the Urgent milestone 2026-04-22 21:04:54 +02:00
Contributor

This proposal isn't dependent on but is closely connected to introduce @FieldEnum and use it for the operand type of builtins that accept a field name #23618, which proposes that @field should be changed to take a @FieldEnum(T) instead of []const u8. One of the comments points out:

Question: how does this apply to @field on a namespace? Currently you'd do @field(struct { const foo = 0; }, "foo"), but under this proposal, it would need to be an enum. Would we get an equivalent @DeclEnum for @field(type, @DeclEnum())?

If @field is changed to @member (instead of being split out into @field and @decl, as suggested by the original #3839), and if the @FieldEnum proposal were to be accepted and pursued, then a corresponding @DeclEnum builtin will be necessary to not lose the ability to access decls, and the @member builtin would need to behave rougly like:

@member(lhs: anytype, comptime member: if (@TypeOf(lhs) == type) @DeclEnum(@TypeOf(lhs)) else @FieldEnum(@TypeOf(lhs))) (member)
const std = @import("std");
test {
    const S = struct {
        x: i32,
        pub const y: i32 = 2;
    };
    const s: S = .{ .x = 1 };
    try std.testing.expectEqual(1, @member(s, .x)); // @as(@FieldEnum(S), .x)
    try std.testing.expectEqual(2, @member(S, .y)); // @as(@DeclEnum(S), .y)
}
This proposal isn't dependent on but is closely connected to [introduce `@FieldEnum` and use it for the operand type of builtins that accept a field name #23618](https://github.com/ziglang/zig/issues/23618), which proposes that `@field` should be changed to take a `@FieldEnum(T)` instead of `[]const u8`. One of the comments points out: > Question: how does this apply to `@field` on a namespace? Currently you'd do `@field(struct { const foo = 0; }, "foo")`, but under this proposal, it would need to be an enum. Would we get an equivalent `@DeclEnum` for `@field(type, @DeclEnum())`? If `@field` is changed to `@member` (instead of being split out into `@field` and `@decl`, as suggested by the original [#3839](https://github.com/ziglang/zig/issues/3839)), and if the `@FieldEnum` proposal were to be accepted and pursued, then a corresponding `@DeclEnum` builtin will be necessary to not lose the ability to access decls, and the `@member` builtin would need to behave rougly like: ```zig @member(lhs: anytype, comptime member: if (@TypeOf(lhs) == type) @DeclEnum(@TypeOf(lhs)) else @FieldEnum(@TypeOf(lhs))) (member) ``` ```zig const std = @import("std"); test { const S = struct { x: i32, pub const y: i32 = 2; }; const s: S = .{ .x = 1 }; try std.testing.expectEqual(1, @member(s, .x)); // @as(@FieldEnum(S), .x) try std.testing.expectEqual(2, @member(S, .y)); // @as(@DeclEnum(S), .y) } ```
Contributor

@FieldType -> @Member should be included in this

`@FieldType` -> `@Member` should be included in this
@member(lhs: anytype, comptime member: if (@TypeOf(lhs) == type) @DeclEnum(@TypeOf(lhs)) else @FieldEnum(@TypeOf(lhs))) (member)
const std = @import("std");
test {
    const S = struct {
        x: i32,
        pub const y: i32 = 2;
    };
    const s: S = .{ .x = 1 };
    try std.testing.expectEqual(1, @member(s, .x)); // @as(@FieldEnum(S), .x)
    try std.testing.expectEqual(2, @member(S, .y)); // @as(@DeclEnum(S), .y)
}

I believe this examples shows a deeper problem with @field or @member: It can be used to access fields and declarations alike. I would rather prefer having 2 builtins: one for fields and one for members. It would disambiguate the two. Of course their could be breakage when one wants to access both field and declarations, but I can figure out a case where one would want that.

> ```zig > @member(lhs: anytype, comptime member: if (@TypeOf(lhs) == type) @DeclEnum(@TypeOf(lhs)) else @FieldEnum(@TypeOf(lhs))) (member) > ``` > > ```zig > const std = @import("std"); > test { > const S = struct { > x: i32, > pub const y: i32 = 2; > }; > const s: S = .{ .x = 1 }; > try std.testing.expectEqual(1, @member(s, .x)); // @as(@FieldEnum(S), .x) > try std.testing.expectEqual(2, @member(S, .y)); // @as(@DeclEnum(S), .y) > } > ``` I believe this examples shows a deeper problem with ```@field``` or ```@member```: It can be used to access fields and declarations alike. I would rather prefer having 2 builtins: one for fields and one for members. It would disambiguate the two. Of course their could be breakage when one wants to access both field and declarations, but I can figure out a case where one would want that.
Contributor

no, imo it only highlights that @member should stay using strings

no, imo it only highlights that `@member` should stay using strings

Does this proposal include the rename of @fieldParentPtr ?

Maybe I would rename it @parentPtrFromMemberPtr ?

Does this proposal include the rename of `@fieldParentPtr` ? Maybe I would rename it `@parentPtrFromMemberPtr` ?

@gschwind I don't think so. The current @field serves two functions: accessing struct fields and accessing namespace declarations. This is the reason it needs to be renamed.

@fieldParentPtr is clearly performing its function; it only applies to fields.

@gschwind I don't think so. The current `@field` serves two functions: accessing struct fields and accessing namespace declarations. This is the reason it needs to be renamed. `@fieldParentPtr` is clearly performing its function; it only applies to fields.

@npc1054657282 ok look good

@npc1054657282 ok look good

Related: #30107

Related: https://codeberg.org/ziglang/zig/issues/30107
Contributor

I would like to work on this, but I need to update zig1.wasm right? I cannot do that because I have a 4GB machine. Would a series of patches that change uses of @field and @FieldType to @member and @Member still be useful?

Edit: Now I have a 16GB machine.

I would like to work on this, but I need to update `zig1.wasm` right? I cannot do that because I have a 4GB machine. Would a series of patches that change uses of `@field` and `@FieldType` to `@member` and `@Member` still be useful? Edit: Now I have a 16GB machine.

Is it possible to rename @field to @access instead? @access(T, "b") would read "through T access an element named b".

Not all usage of a.b can be seen as a member access.
left aside the a.b(c) syntax can mean call a function stored in a's field b with c as argument OR call a function associated with the type of a with a and c as arguments, this is can be only decided after sema. IMO only the first one can/should be replicated with @access(a, "b")(c).

If in a.b, b should read "member of a", what is a "member"?
"Something part of some other thing"? : fields for object instances, decl for type/namespace.
Or "Something closely associated with some other thing"? : arr.len, is len part of the type or the instance ? why can we access it through the instance?
Or better "Something that can be ACCESSED through some other thing"! : can imply constness propagation, implicit pointer dereference ...

Just like semantic considerations are leading @field -> @member (#35323 (comment)) others will probably plague the new @member (#35826). We can decide later what can be accessed through what or not. It seems MORE FUTURE PROOF to name it @access which make explicit that we want to access something through another thing, the accessed things may be a field, a declaration or else ...

Is it possible to rename `@field` to `@access` instead? `@access(T, "b")` would read "through T access an element named `b`". Not all usage of `a.b` can be seen as a member access. left aside the `a.b(c)` syntax can mean call a function stored in `a`'s field `b` with `c` as argument OR call a function associated with the type of `a` with `a` and `c` as arguments, this is can be only decided after sema. IMO only the first one can/should be replicated with `@access(a, "b")(c)`. If in `a.b`, `b` should read "member of `a`", what is a "member"? "Something part of some other thing"? : fields for object instances, decl for type/namespace. Or "Something closely associated with some other thing"? : `arr.len`, is `len` part of the type or the instance ? why can we access it through the instance? Or better "Something that can be ACCESSED through some other thing"! : can imply constness propagation, implicit pointer dereference ... Just like semantic considerations are leading `@field` -> `@member` (https://codeberg.org/ziglang/zig/issues/35323#issuecomment-24779745) others will probably plague the new `@member` (https://codeberg.org/ziglang/zig/issues/35826). We can decide later what can be accessed through what or not. It seems MORE FUTURE PROOF to name it `@access` which make explicit that we want to access something through another thing, the accessed things may be a field, a declaration or else ...
Sign in to join this conversation.
No milestone
No assignees
9 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#32029
No description provided.