-
Notifications
You must be signed in to change notification settings - Fork 205
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
Strawman Nominal Union types proposal: Can two unions be assigned to each other? #2695
Comments
Correct. As I envisioned it (and note the caveat that the type system doesn't actually work!) those two would be different, unrelated types. That's the "nominal" part of the definition, each declaration introduces a name, and each name is its own type. Types with different names are not the same. Types with different names are also only related if declared to be so. You can write The intended advantage over general, structural union types is that all the different union types are known at compile time, and they all have a name. Trying to do the least upper bound of Maybe it's a fool's game to prevent stuctural union types, because someone can just define union U2<S, T> = S | T;
union U3<R, S, T> = R | S | T;
// or even: union U3<R, S, T> = U2<R, S>| U2<S, T> | U2<R, T>;
// etc. up to 6, maybe 9. That should be enough for everybody. and then everybody could just use those as their structural-like union types. We'd still not make the least upper bound of |
I see. In that case, what is the true difference with relying on the view class Union2<A, B> {
Union2.first(A a);
Union2.second(B b);
} or whatever the syntax is. It's not a flawless approach. It has some issues like:
And it seems like this proposal would have difficulties solving those issues. I personally don't mind too much those restrictions with the view approach because it's designed more as a workaround than a true solution to unions (with the true target being sealed classes). But this proposal would involve actual language features specifically designed for unions. Of course, I'd love to be wrong. I don't want to sound overly negative. I'm happy to see proposals for union support! |
Hmm, I hadn't considered that union Foo<T, S> = Set<T> | List<S>; would not be the same if you flipped the type arguments. We get co-/contra-variance from the parameter positions, but we won't try to see if the union-types could be equivalent if you reshuffled them. I'd be fine with using views/inline classes for the same thing, but they are defined in terms of a "representation type" that covers all the possible values that the view can be used on. Using it for a union means effectively having a union type as the representation type, and then we're back to having to give that union a name. |
Hello! I was reading through the working/union-types/nominative-union-types.md proposal. Interesting stuff, but it got me wondering:
Would we be able to do the following?
In fact, can we even do the following?
This is something that sounds fundamental to unions for me. But the proposal to me seems to imply that this wouldn't work.
My understanding is that the proposal would be equivalent to creating a new type every time. So two compatible unions in a different language wouldn't be compatible using this proposal.
Is that correct?
The text was updated successfully, but these errors were encountered: