diff --git a/doc/langref.html.in b/doc/langref.html.in index 2ad86d653e..374fbfcde5 100644 --- 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#}
{#syntax#}@field(lhs: var, comptime field_name: []const u8) (field){#endsyntax#}
- 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#}. +
Performs field access by a compile-time string.
+ {#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#}