Shorten @field documentation and add an example

This commit is contained in:
Vesa Kaihlavirta 2019-09-04 20:02:31 +03:00 committed by Andrew Kelley
parent fe153ad2a4
commit 847a262efd

View File

@ -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#}