mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
Shorten @field documentation and add an example
This commit is contained in:
parent
fe153ad2a4
commit
847a262efd
@ -6976,10 +6976,28 @@ export fn @"A function name that is a complete sentence."() void {}
|
||||
|
||||
{#header_open|@field#}
|
||||
<pre>{#syntax#}@field(lhs: var, comptime field_name: []const u8) (field){#endsyntax#}</pre>
|
||||
<p>Preforms field access equivalent to {#syntax#}lhs.field_name{#endsyntax#}, except instead
|
||||
of the field {#syntax#}"field_name"{#endsyntax#}, it accesses the field named by the string
|
||||
value of {#syntax#}field_name{#endsyntax#}.
|
||||
<p>Performs field access by a compile-time string.
|
||||
</p>
|
||||
{#code_begin|test#}
|
||||
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);
|
||||
}
|
||||
{#code_end#}
|
||||
|
||||
{#header_close#}
|
||||
|
||||
{#header_open|@fieldParentPtr#}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user