Super excited to be using type unions in the 0.9.0 release - thanks guys!
Would it be possible to include some examples in the Union Type docs on how one might go about pattern matching over type unions, where these examples include cases resulting in None/Any or some overlap?
type JSON = Nat or Text or HashMap or ...;
func toJSON(json: JSON): Text {
switch(json) {
case Nat n { ... };
case Text t { ... };
case HashMap hm { ... this could be recursive and call toJSON depending on the k/v type };
}
}
If this is the case, then where does the value from type unions come in? Under what scenarios would it make more sense to choose to use a type union over a predetermined wrapped variant type?
Right, but the same thing applies. You have some existing types and want to say “this or that”/“this and that” without having to create a new named type.
As the documentation says, it’s more useful when the types are compatible through sub typing.
I see, ok so then type unionsintersections essentially favor the creation of new types through composition/reuse of smaller types. I’m assuming these would be usually combination of record types, for example something like:
type UserPermissions = {
#createPost;
#editPost;
#deletePost;
...
};
type AdminPermissions = {
#banUser;
#createChannel;
} and UserPermissions;