-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support runtime union types with untagged enums #4734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
3f4ca60 to
383f625
Compare
383f625 to
7e992cf
Compare
|
Just to reflect some private concerns here: this seems like an extension of new syntax sugar that I know @daxpedda wanted to try and avoid in the core, so I'm going to defer to him for review. E.g. once we land this, what if users might want all other Serde enum representations built into wasm-bindgen as well? It seems we might want to answer a question of where we draw a line between built-in serialization and things covered by serde-wasm-bindgen integration before adding features from said subset. There are definitely valid reasons to add some integrations to the core, at least for optimisations alone as serde-wasm-bindgen doesn't have as much access to type information at compile-time as wasm-bindgen itself does, but we need to make sure we don't paint ourselves in the corner and on the hook for maintenance of even larger API area with all the semver difficulties that wasm-bindgen already comes with. Maybe there are hooks we could expose instead to make tools like serde-wasm-bindgen more efficient or support more types, so that the core remains lean? I don't know the answer, but something worth exploring. Offtop: what you're adding here is rather the untagged enums not tagged ones. |
7e992cf to
c0c6a7a
Compare
c0c6a7a to
4845e0c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #2088 and #2407 for previous discussions.
I definitely have the concerns pointed out by RReverser, these are also laid out more in #2088.
However, I don't think its realistic that we are getting any sort of better output customization in wasm-bindgen in this version, because its just going be too difficult to design. Maybe we can live with an ad-hoc implementation just for this version, but I'm apprehensive about it.
@guybedford did you ever take a look at tsify? Again, I don't think wasm-bindgen is offering something here, in the end its all a JsValue and we aren't providing an optimized conversion. The TS part can be handled with typescript_type.
I think it would be good to discuss this in the upcoming meeting and how we want to proceed in general.
f0adc28 to
771867a
Compare
17c5a0a to
b114197
Compare
CodSpeed Performance ReportMerging #4734 will not alter performanceComparing Summary
|
b114197 to
0f9ca45
Compare
This extends enum support to work with singular untagged enums as a form of union type, allowing a representation for TypeScript union types using runtime generics in wasm bindgen.
Per standard generics, we treat
JsValueas the target, then apply runtime checks to convert from JS into the appropriate type using theTryFromJsValuetrait. Each tagged field of the enum is attempted in succession until one succeeds.This is based to #4714 which extends all types into this runtime check, allowing for this implementation.
With this, it is possible to type JS functions like:
where we can write:
and have it work correctly for both directions of bindgen.