Skip to content

Commit 5a91dbc

Browse files
committed
allow const to be passed to @Hasfield()
Actually include the tests I wrote
1 parent d1f9b81 commit 5a91dbc

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

doc/langref.html.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6868,7 +6868,7 @@ fn add(a: i32, b: i32) i32 { return a + b; }
68686868
</p>
68696869
{#header_close#}
68706870
{#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>
68726872
<p>Returns if the field name of a struct, union, or enum exists.</p>
68736873
<p>
68746874
The result is a compile time constant.

test/stage1/behavior/hasfield.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)