Commit 847a262efd

Vesa Kaihlavirta <vegai@iki.fi>
2019-09-04 19:02:31
Shorten @field documentation and add an example
1 parent fe153ad
Changed files (1)
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#}