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
Union types #11
Union types #11
Conversation
|
Is this ever going to be considered? What's the status? |
I'm willing to consider a |
Can you also consider |
That should be the same as |
If that's done, can the error message be improved/customized?
isn't very readable (https://try.haxe.org/#212cB) compared to |
I don't think just EitherType needs any special syntax. I'd go with either "proper" type unions or nothing here. Regarding TS-like type unions (with cool recursive union field types and stuff), I don't think it's a good idea to have those as language feature in Haxe, because it only works "nicely" in dynamic targets, while on static targets it would produce a lot of reflection field lookups in many cases (even if we make compiler smart about common base classes/interfaces). For TypeScript they make sense because their goal is to provide typed way to deal with any JS craziness, but for Haxe, these are only needed to interface with dynamic extern APIs, such as JS, and EitherType provide the way to deal with it. So, again, I'm not convinced that this even deserves a special syntax. PS I sometimes do |
I once had an idea that it would be useful in some cases to have pluggable |
Well I think I ask this because I mainly use Haxe for JS. Q: If its only for externs and just wrapper around the EitherType does it still add something? Otherwise lets close this request. I understand it requires more dynamic kinds to deal with, which seem to be hazzle (for like optimizations etc). I think it would be inconsistent for Haxe to only have it in externs, no? For me union types was to provide a more elegant looking solution to work with dynamic stuff and could provide somewhat more flexible constraints. |
Values that can be multiple different types is a very dynamic / JavaScripty idea. I agree with @RealyUniqueName - I've dealt with Via the magic of the abstract, note that:
IMO, Edit to add: OneOf isn't all roses. To actually use it, you have to either 1) use switch as shown, 2) assign it to a typed variable, 3) pass it to a typed function arg, or 4) use a type check tell the compiler what type is should be:
Another gotcha -- |
This can be mitigated by a @:to inline function swap():OneOf<B, A> return switch this {case Right(b): Left(b); case Left(a): Right(a);} |
I just read @jcward and @kevinresol comments. I agree with them. Should we close now or does anyone else think we should proceed here? |
I'll go ahead and close this. Still open to the idea of |
This proposes introduction of union types as a alternative to Either and EitherType. A union type describes a value that can be one of several types.
I think this is a valuable addition to the type system of Haxe, especially when dealing with JavaScript. What I propose is most likely missing details, but the idea should be clear. I leave these details up to the our skilled team members. Looking forward to your thoughts.
I'm inspired by the union types of TypeScript, which are described here https://www.typescriptlang.org/docs/handbook/advanced-types.html
» Rendered version here