I want to encode opcode data as a enum with payload. The data I need to store is index and number of arguments.
const RawOp = struct {
idx: u8,
name: []const u8,
n_arg: u8,
};
const rawOps: []const RawOp = &.{
.{ 1, "add", 2 },
.{ 2, "sub", 2 },
};
I'd like to do in the code later:
switch(@enumFromInt(ip)) {
.add => |n_args| {
const idx = ip;
},
.sub => |n_args| ...
}
What's the idiomatic way to do it? Preferably a one where all data is defined in one place.
I could do enum with backing integer for index and store number of args separately but maybe there's a better way.