authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2021-02-01 09:11:06-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-01 12:29:50-08:00
log5e81b048a0966815cc1911e33144ed6ffad37ba1
tree0fd0c7468a66f3c5620b1e6b9046a7f9e66b4534
parent1517ed0a5ede7da476fdffa8bfe74dab3d1e3810

docs: Clarify that @field can work on declarations


1 files changed, 14 insertions(+), 3 deletions(-)

doc/langref.html.in+14-3
......@@ -7533,19 +7533,21 @@ export fn @"A function name that is a complete sentence."() void {}
75337533
75347534 {#header_open|@field#}
75357535 <pre>{#syntax#}@field(lhs: anytype, comptime field_name: []const u8) (field){#endsyntax#}</pre>
7536 <p>Performs field access by a compile-time string.
7536 <p>Performs field access by a compile-time string. Works on both fields and declarations.
75377537 </p>
75387538 {#code_begin|test#}
75397539const std = @import("std");
75407540
75417541const Point = struct {
75427542 x: u32,
7543 y: u32
7543 y: u32,
7544
7545 pub var z: u32 = 1;
75447546};
75457547
75467548test "field access by string" {
75477549 const expect = std.testing.expect;
7548 var p = Point {.x = 0, .y = 0};
7550 var p = Point{ .x = 0, .y = 0 };
75497551
75507552 @field(p, "x") = 4;
75517553 @field(p, "y") = @field(p, "x") + 1;
......@@ -7553,6 +7555,15 @@ test "field access by string" {
75537555 expect(@field(p, "x") == 4);
75547556 expect(@field(p, "y") == 5);
75557557}
7558
7559test "decl access by string" {
7560 const expect = std.testing.expect;
7561
7562 expect(@field(Point, "z") == 1);
7563
7564 @field(Point, "z") = 2;
7565 expect(@field(Point, "z") == 2);
7566}
75567567 {#code_end#}
75577568
75587569 {#header_close#}