#How do I check if 2 unions have the same active type?

1 messages · Page 1 of 1 (latest)

cerulean crest
#

Given 2 variables of type U (union(enum){ num: i32, str: []const u8 };). How do I check that:

var a = U{.num=3};
var b = U{.str="something"};

// How to check if a and b have the same active type? kinda like this:
// if(a.curType == b.curType)

From what I know types are tagged with an enum(backed by an int) so there should be a way to check this I assume

severe river
#

using switch

#
switch(a) {
 .num => {},
}
leaden ember
#

if (std.meta.activeTag(a) == b)

severe river
#

Didn't know about it

#

thanks!

sand anvil
#

Fengb's suggestion comes from the fact that b == .num works - which in turns works because unions have a backing enum with values that correspond to the union variant names.
activeTag(a) returns .num if U.num is set in a.