From 5a91dbc16cbe20a95aafb6b3221c4a5a07d9b117 Mon Sep 17 00:00:00 2001 From: Shawn Landden Date: Mon, 27 May 2019 00:35:35 -0500 Subject: [PATCH] allow const to be passed to @hasField() Actually include the tests I wrote --- doc/langref.html.in | 2 +- test/stage1/behavior/hasfield.zig | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/stage1/behavior/hasfield.zig diff --git a/doc/langref.html.in b/doc/langref.html.in index 02375639fb..3bb38c0437 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -6868,7 +6868,7 @@ fn add(a: i32, b: i32) i32 { return a + b; }

{#header_close#} {#header_open|@hasField#} -
{#syntax#}@hasField(comptime T: type, comptime name: []u8) bool{#endsyntax#}
+
{#syntax#}@hasField(comptime T: type, comptime name: []const u8) bool{#endsyntax#}

Returns if the field name of a struct, union, or enum exists.

The result is a compile time constant. diff --git a/test/stage1/behavior/hasfield.zig b/test/stage1/behavior/hasfield.zig new file mode 100644 index 0000000000..0e18cb19b5 --- /dev/null +++ b/test/stage1/behavior/hasfield.zig @@ -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); +}