-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow Singleton types in Union types #1551
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
Comments
Note that singleton types in unions can still slip through the cracks: scala> def or[T](x: T | Int) = x
or: [T](x: T | Int)T | Int
scala or["foo"]("bar")
res13: String | Int = bar It's probably not worth trying to disallow this case before we try to make singleton types in unions actually work. |
I uncovered another valid -- I think -- use-case while experimentitng with This is intended to be a linked-list-like datastructure, but with alternating types, and it might make a good test case, but currently doesn't compile due to the restriction. enum Fence[+T, +S] {
case End
case Post(value: T, next: Panel[T, S] | End.type)
case Panel(value: S, next: Post[T, S])
}
import Fence._
val fence = Post(1, Panel("two", Post(3, End))) |
Would have been nice to do: |
Just a curiosity, the object Get2As1 {
class OrIntroFn[T, U, TU >: T|U]{
type V = TU
def tuToV(): (TU => V) = p => p
}
class Or11X[X] extends OrIntroFn[1&1, X, (1&1|X)]{
def get2as11X:V = tuToV()(2)
}
class Or11Nothing extends Or11X[Nothing]
def get2as1:1 = new Or11Nothing().get2as11X
def main(a:Array[String]) = {
println(get2as1) // prints 2
val one:1 = get2as1
println(one) // prints 1
}
} |
In TypeScript this isn't an issue: type ABC = 'A' | 'B' | 'C'
type A2F = ABC | 'D' | 'E' | 'F'
foo(x: A2F) {...}
foo('F') // ok
foo('G') // fails Union types combined with string literals are quite powerful, not to mention concise -- above approach is used all the time in TypeScript -- would be great if this restriction were lifted. We can perhaps workaround the issue with def foo(x: "A" | "B" | "C") = ... useful for one-off cases where you want type safety but not be bothered with introducing a separate type. |
Fixed by #6299 ✨ |
#1550 disallows singleton types in union types to avoid the problems uncovered by #829. But it would be good to allow them, if we can find a solid solution.
The current scheme is: (1) singleton types in unions are not allowed. (2) Therefore, when taking the lub, we widen every singleton type. Thats simple and consistent, but also restrictive.
The text was updated successfully, but these errors were encountered: