Description
Currently a variant with multiple constructors carrying payloads are compiled to array with a tag
field (an int representing the constructor). This isn't ideal, because serializing the variant* loses that tag
field (extra array fields are ignored), and more importantly, various JS tools such as Jest compare the array and neglect the tag (it's rather common to ignore the fact that an array might have extra fields), so e.g. the test assertions are wrong: assert.equal(Foo(1), Bar(1))
passes.
For friendlier interop with existing JS systems, and to keep things simple, I'd say we could:
- Change the representation to be
[tag, payload1, payload2, ...]
. No extra allocation. - Change the representation to be
{tag, payload: someTypeHere}
. Extra alloc for e.g. constructors with many payload. But more friendly to reading I guess?
* We shouldn't serialize & deserialize the variant and expect things to work, but for temporary iteration this might be fine. Plus, helps a little for e.g. logging where you don't really deserialize.