@field

@field

@field(lhs: var, 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 assert = std.debug.assert;
    var p = Point {.x = 0, .y = 0};

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

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