File tree 2 files changed +31
-1
lines changed 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -6868,7 +6868,7 @@ fn add(a: i32, b: i32) i32 { return a + b; }
6868
6868
</p>
6869
6869
{#header_close#}
6870
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
6872
<p>Returns if the field name of a struct, union, or enum exists.</p>
6873
6873
<p>
6874
6874
The result is a compile time constant.
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments