Some Tricks from the Scrapscript Compiler
bernsteinbear.com
bernsteinbear.com
No, they are able to do that because they have static typing and they don't have to distinguish between values of "type A = FooA | BarA" and "type B = FooB | BarB" at run-time, so they can represent FooA, FooB, 0, false, unit etc. all the same. They can't do the same with their "extensible variants" though, so those IIRC are encoded as objects.
Wait, how does Scrapscript distinguish between values of different types? Say,
. scoop :
#vanilla
#chocolate
#strawberry
. f =
| #vanilla -> 1
| #false -> 2
| x -> 999
Is that legal? If yes, how does it work?Your example is legal (modulo the constraint that every tag must have an associated value) and works because we store the tag either in the pointer or the tag field.
[0] https://schu.be/til-ocaml-polymorphic-variant-hash-collision...
Edit: oh, it's unique per compilation and isn't meant to be shared around. The variants are shared with string names in flat scraps (small serialized ASTs)
You see, those are eternal (well, perennial) problems with distributed code management and every language/computing platform has to deal with them somehow, so that's why I'm interested in what your approach is, especially so since you aim to "solve the software sharability problem".