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 {}...@@ -7533,19 +7533,21 @@ export fn @"A function name that is a complete sentence."() void {}
75337533
7534 {#header_open|@field#}7534 {#header_open|@field#}
7535 <pre>{#syntax#}@field(lhs: anytype, comptime field_name: []const u8) (field){#endsyntax#}</pre>7535 <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.
7537 </p>7537 </p>
7538 {#code_begin|test#}7538 {#code_begin|test#}
7539const std = @import("std");7539const std = @import("std");
75407540
7541const Point = struct {7541const Point = struct {
7542 x: u32,7542 x: u32,
7543 y: u327543 y: u32,
7544
7545 pub var z: u32 = 1;
7544};7546};
75457547
7546test "field access by string" {7548test "field access by string" {
7547 const expect = std.testing.expect;7549 const expect = std.testing.expect;
7548 var p = Point {.x = 0, .y = 0};7550 var p = Point{ .x = 0, .y = 0 };
75497551
7550 @field(p, "x") = 4;7552 @field(p, "x") = 4;
7551 @field(p, "y") = @field(p, "x") + 1;7553 @field(p, "y") = @field(p, "x") + 1;
...@@ -7553,6 +7555,15 @@ test "field access by string" {...@@ -7553,6 +7555,15 @@ test "field access by string" {
7553 expect(@field(p, "x") == 4);7555 expect(@field(p, "x") == 4);
7554 expect(@field(p, "y") == 5);7556 expect(@field(p, "y") == 5);
7555}7557}
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}
7556 {#code_end#}7567 {#code_end#}
75577568
7558 {#header_close#}7569 {#header_close#}