Skip to content

Add union type - #766

Open
bvisness wants to merge 2 commits into
WebAssembly:mainfrom
bvisness:union
Open

bvisness wants to merge 2 commits into
WebAssembly:mainfrom
bvisness:union

Conversation

@bvisness

@bvisness bvisness commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Adds support for union (a variant of variant). The design of unions is mostly unchanged from what @eqrion originally proposed (maybe modulo some of the mapping from types to distinguishable kinds).

Resolves #724.

One thing I wanted to discuss is the distinguishability rules (which may eventually be reused for overloads). It's really hard to find a set of "distinguishable kinds" for each type that will actually avoid conflicts in various languages without being totally useless. The danger, in my opinion, is "kind creep", where you keep adding more distinguishable kinds to each component type, e.g. adding the list kind to the component map type because maps are frequently iterable in source languages. This reduces ambiguity for bindings, but makes unions much less useful.

In my opinion the right way to handle this is to be stricter about types when in a "distinguishable context". You can see this reflected in the updated JS API. A plain old list<string> parameter will accept any JS iterable (in TypeScript terms, Iterable<string>), whereas a union{ list<string>, ... } parameter requires a JS array (Array<string>). In other words, the runtime would literally run Array.isArray to decide if the value is "list-like", rather than checking for Symbol.iterator. This should only be a modest complexity cost for devs and one that TypeScript will catch right away. And importantly it ensures that union parameters should round-trip nicely; e.g. a union{ list, map } will flow out to JS as an Array or Map and back to components as a list or map, without any awkward crossover.

@bvisness
bvisness marked this pull request as ready for review November 24, 2026 17:29
@bvisness
bvisness requested a review from lukewagner November 24, 2026 17:29
Just a bunch of drive-by updates made as part of union/overload work.
@bvisness bvisness changed the title Add unions and overloads Add union type Nov 25, 2026
@bvisness

Copy link
Copy Markdown
Collaborator Author

I have updated this PR to only contain unions, and have moved overloads to #774.

@bvisness bvisness mentioned this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anonymous union as a specialization of variant to support common web APIs

1 participant