Get union value type by enum

pub const Node = struct {
	kind: Kind,
    props: Props

	pub fn init(comptime kind: Kind, prop: ?) void {

	}

	pub const Kind = enum {
		A,
		B,
		C
	};

	pub const Props = union(Kind) {
		A: A,
		B: B,
		C: C,
	} ;

	pub const A = struct {
		
	};
	pub const B = struct {

	};
	pub const C = struct {

	};
};

Can i get the struct of prop by kind type?

I thought use it like:

Node.init(.A, .{})

I already tried:

pub fn init(comptime kind: Kind, prop: @TypeOf(@field(Props, @tagName(kind))))

I think you are looking for std.meta.TagPayload here are some examples that use it:

2 Likes

Is this what you wanted?

pub fn init(prop:  Props) Node {
 return .{ .kind = props };
}

A tagged union coerces into the tag type.

thanks, TagPayload will work on my case