Encoding ADT's robustly #121
Hi! I noticed that roy uses 'instanceof' quite pervasively, especially for pattern-matching ADT's.
Instanceof tends to have trouble when doing cross-framescripting and I suspect also in webworkers etc.
One possible solution is to leverage the internal [[CLASS]] property instead as suggested by various by people around the web:
However, there is also the problem of serializing ADT's to JSON, which I think will be pretty common when communicating with a remote server. I'd like to suggest perhaps considering an encoding that includes the constructors?
might be represented as
or perhaps differentiating between constructors that take multiple arguments and constructors that take a single argument
on the other hand, simply using arrays might be faster?
...in which case the code to match the nested constructors might need to do something like
Which I suspect should also be pretty efficient given that Array.isArray is a native function (pure speculation though).
P.S. Perhaps it is useful to support a similar pattern matching mechanism for Lists and ADT's in order to make the generated js look uniform?
The [[Class]] solution has its own problem, which is that it's only doing string comparison. In contrast,
I propose that serialization and deserialization with type information is not something Roy should try to automate at a language level. Instead, that should be left to libraries such as HydrateJS.