Skip to content

Commit

Permalink
allow const to be passed to @Hasfield()
Browse files Browse the repository at this point in the history
Actually include the tests I wrote
  • Loading branch information
shawnl committed May 27, 2019
1 parent d1f9b81 commit 5a91dbc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -6868,7 +6868,7 @@ fn add(a: i32, b: i32) i32 { return a + b; }
</p>
{#header_close#}
{#header_open|@hasField#}
<pre>{#syntax#}@hasField(comptime T: type, comptime name: []u8) bool{#endsyntax#}</pre>
<pre>{#syntax#}@hasField(comptime T: type, comptime name: []const u8) bool{#endsyntax#}</pre>
<p>Returns if the field name of a struct, union, or enum exists.</p>
<p>
The result is a compile time constant.
Expand Down
30 changes: 30 additions & 0 deletions test/stage1/behavior/hasfield.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const expect = @import("std").testing.expect;
const builtin = @import("builtin");

test "@hasField" {
const struc = struct {
a: i32,
b: []u8,
};
expect(@hasField(struc, "a") == true);
expect(@hasField(struc, "b") == true);
expect(@hasField(struc, "non-existant") == false);

const unin = union {
a: u64,
b: []u16,
};
expect(@hasField(unin, "a") == true);
expect(@hasField(unin, "b") == true);
expect(@hasField(unin, "non-existant") == false);

const enm = enum {
a,
b,
};
expect(@hasField(enm, "a") == true);
expect(@hasField(enm, "b") == true);
expect(@hasField(enm, "non-existant") == false);

expect(@hasField(builtin, "os") == true);
}

0 comments on commit 5a91dbc

Please sign in to comment.