@field

@field

@field(lhs: anytype, comptime field_name: []const u8) (field)

Performs field access by a compile-time string.

test.zig

const std = @import("std");

const Point = struct {
    x: u32,
    y: u32
};

test "field access by string" {
    const expect = std.testing.expect;
    var p = Point {.x = 0, .y = 0};

    @field(p, "x") = 4;
    @field(p, "y") = @field(p, "x") + 1;

    expect(@field(p, "x") == 4);
    expect(@field(p, "y") == 5);
}
``` ```
$ zig test test.zig
1/1 test "field access by string"... OK
All 1 tests passed.