mirror of
https://github.com/ziglang/zig.git
synced 2026-02-14 13:30:45 +00:00
docs: Clarify that @field can work on declarations
This commit is contained in:
parent
1517ed0a5e
commit
5e81b048a0
@ -7533,19 +7533,21 @@ export fn @"A function name that is a complete sentence."() void {}
|
||||
|
||||
{#header_open|@field#}
|
||||
<pre>{#syntax#}@field(lhs: anytype, comptime field_name: []const u8) (field){#endsyntax#}</pre>
|
||||
<p>Performs field access by a compile-time string.
|
||||
<p>Performs field access by a compile-time string. Works on both fields and declarations.
|
||||
</p>
|
||||
{#code_begin|test#}
|
||||
const std = @import("std");
|
||||
|
||||
const Point = struct {
|
||||
x: u32,
|
||||
y: u32
|
||||
y: u32,
|
||||
|
||||
pub var z: u32 = 1;
|
||||
};
|
||||
|
||||
test "field access by string" {
|
||||
const expect = std.testing.expect;
|
||||
var p = Point {.x = 0, .y = 0};
|
||||
var p = Point{ .x = 0, .y = 0 };
|
||||
|
||||
@field(p, "x") = 4;
|
||||
@field(p, "y") = @field(p, "x") + 1;
|
||||
@ -7553,6 +7555,15 @@ test "field access by string" {
|
||||
expect(@field(p, "x") == 4);
|
||||
expect(@field(p, "y") == 5);
|
||||
}
|
||||
|
||||
test "decl access by string" {
|
||||
const expect = std.testing.expect;
|
||||
|
||||
expect(@field(Point, "z") == 1);
|
||||
|
||||
@field(Point, "z") = 2;
|
||||
expect(@field(Point, "z") == 2);
|
||||
}
|
||||
{#code_end#}
|
||||
|
||||
{#header_close#}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user