| author | |
| committer | |
| log | 5a91dbc16cbe20a95aafb6b3221c4a5a07d9b117 |
| tree | 1c8fd5542d40d8aea05fff58db57c50dc053e39d |
| parent | d1f9b8184d61674a77424f026adfef2752d1dfdd |
Actually include the tests I wrote2 files changed, 31 insertions(+), 1 deletions(-)
doc/langref.html.in+1-1| ... | @@ -6868,7 +6868,7 @@ fn add(a: i32, b: i32) i32 { return a + b; } | ... | @@ -6868,7 +6868,7 @@ fn add(a: i32, b: i32) i32 { return a + b; } |
| 6868 | </p> | 6868 | </p> |
| 6869 | {#header_close#} | 6869 | {#header_close#} |
| 6870 | {#header_open|@hasField#} | 6870 | {#header_open|@hasField#} |
| 6871 | <pre>{#syntax#}@hasField(comptime T: type, comptime name: []u8) bool{#endsyntax#}</pre> | 6871 | <pre>{#syntax#}@hasField(comptime T: type, comptime name: []const u8) bool{#endsyntax#}</pre> |
| 6872 | <p>Returns if the field name of a struct, union, or enum exists.</p> | 6872 | <p>Returns if the field name of a struct, union, or enum exists.</p> |
| 6873 | <p> | 6873 | <p> |
| 6874 | The result is a compile time constant. | 6874 | The result is a compile time constant. |
test/stage1/behavior/hasfield.zig created+30| ... | @@ -0,0 +1,30 @@ | ||
| 1 | const expect = @import("std").testing.expect; | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | |||
| 4 | test "@hasField" { | ||
| 5 | const struc = struct { | ||
| 6 | a: i32, | ||
| 7 | b: []u8, | ||
| 8 | }; | ||
| 9 | expect(@hasField(struc, "a") == true); | ||
| 10 | expect(@hasField(struc, "b") == true); | ||
| 11 | expect(@hasField(struc, "non-existant") == false); | ||
| 12 | |||
| 13 | const unin = union { | ||
| 14 | a: u64, | ||
| 15 | b: []u16, | ||
| 16 | }; | ||
| 17 | expect(@hasField(unin, "a") == true); | ||
| 18 | expect(@hasField(unin, "b") == true); | ||
| 19 | expect(@hasField(unin, "non-existant") == false); | ||
| 20 | |||
| 21 | const enm = enum { | ||
| 22 | a, | ||
| 23 | b, | ||
| 24 | }; | ||
| 25 | expect(@hasField(enm, "a") == true); | ||
| 26 | expect(@hasField(enm, "b") == true); | ||
| 27 | expect(@hasField(enm, "non-existant") == false); | ||
| 28 | |||
| 29 | expect(@hasField(builtin, "os") == true); | ||
| 30 | } | ||