From 5e81b048a0966815cc1911e33144ed6ffad37ba1 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Mon, 1 Feb 2021 09:11:06 -0800 Subject: [PATCH] docs: Clarify that @field can work on declarations --- doc/langref.html.in | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index f75fc351d9..7759e26f3b 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -7533,19 +7533,21 @@ export fn @"A function name that is a complete sentence."() void {} {#header_open|@field#}
{#syntax#}@field(lhs: anytype, comptime field_name: []const u8) (field){#endsyntax#}
-

Performs field access by a compile-time string. +

Performs field access by a compile-time string. Works on both fields and declarations.

{#code_begin|test#} const std = @import("std"); const Point = struct { x: u32, - y: u32 + y: u32, + + pub var z: u32 = 1; }; test "field access by string" { const expect = std.testing.expect; - var p = Point {.x = 0, .y = 0}; + var p = Point{ .x = 0, .y = 0 }; @field(p, "x") = 4; @field(p, "y") = @field(p, "x") + 1; @@ -7553,6 +7555,15 @@ test "field access by string" { expect(@field(p, "x") == 4); expect(@field(p, "y") == 5); } + +test "decl access by string" { + const expect = std.testing.expect; + + expect(@field(Point, "z") == 1); + + @field(Point, "z") = 2; + expect(@field(Point, "z") == 2); +} {#code_end#} {#header_close#}