I’m trying to represent a dynamically typed value in Zig, and need to be able to check if two of such values are equal, both in type and in value. I have a function that seems to work but want to know if there is any more efficient way to do this, rather than having a potentially long switch statement for every variant. Thanks.
Aside from using a library function, yes, you probably want multiple prongs of a switch statement. At least for numeric comparisons, since comparing floats is not a straightforward check on whether the bits are equal, there is also the fact that -0 and +0 are equal, NaN’s always being unequal to everything, even themselves, and maybe there’s more. So when you do a == b on float types it is going to give you a completely different instruction that couldn’t be used for any other type.
You could try an inline else though to have Zig fill in the details.