zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 847a262efdf5f3a359b00f13c101236dc0747f1b (tree)
parent fe153ad2a435e26f9904f05858232305bdffd3ac
Author: Vesa Kaihlavirta <vegai@iki.fi>
Date:   Wed,  4 Sep 2019 20:02:31 +0300

Shorten @field documentation and add an example

Diffstat:
Mdoc/langref.html.in | 24+++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/doc/langref.html.in b/doc/langref.html.in @@ -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#}