| ... | ... | @@ -6976,10 +6976,28 @@ export fn @"A function name that is a complete sentence."() void {} |
| 6976 | 6976 | |
| 6977 | 6977 | {#header_open|@field#} |
| 6978 | 6978 | <pre>{#syntax#}@field(lhs: var, comptime field_name: []const u8) (field){#endsyntax#}</pre> |
| 6979 | | <p>Preforms field access equivalent to {#syntax#}lhs.field_name{#endsyntax#}, except instead |
| 6980 | | of the field {#syntax#}"field_name"{#endsyntax#}, it accesses the field named by the string |
| 6981 | | value of {#syntax#}field_name{#endsyntax#}. |
| 6979 | <p>Performs field access by a compile-time string. |
| 6982 | 6980 | </p> |
| 6981 | {#code_begin|test#} |
| 6982 | const std = @import("std"); |
| 6983 | |
| 6984 | const Point = struct { |
| 6985 | x: u32, |
| 6986 | y: u32 |
| 6987 | }; |
| 6988 | |
| 6989 | test "field access by string" { |
| 6990 | const assert = std.debug.assert; |
| 6991 | var p = Point {.x = 0, .y = 0}; |
| 6992 | |
| 6993 | @field(p, "x") = 4; |
| 6994 | @field(p, "y") = @field(p, "x") + 1; |
| 6995 | |
| 6996 | assert(@field(p, "x") == 4); |
| 6997 | assert(@field(p, "y") == 5); |
| 6998 | } |
| 6999 | {#code_end#} |
| 7000 | |
| 6983 | 7001 | {#header_close#} |
| 6984 | 7002 | |
| 6985 | 7003 | {#header_open|@fieldParentPtr#} |