-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Type inference doesn't work correctly for union types used with extension method #18626
Comments
I'm not sure that this is a bug, despite it being unfortunate and a good argument being made on how those other variants work. Singleton types of non-case objects are very brittle - I believe if you make Cat and Dog case objects, then it should continue to work. |
I tried the following: sealed trait Animal
object Animal {
case object Cat extends Animal
case object Dog extends Animal
} But it results in the same error. Is it what you meant? Also, this seems to be a similar to #18551, isn't it? In both situations, the problem is that a union of case objects is widened to it parent type. Setting the type parameter of .element[Mammal](mammals) |
I'm fine with helping compiler a bit as I don't use union of case objects that often. However, it's a bummer that among many ways to do the same, results 1..3, the most popular one is not working as one expects. If fixing it is problematic, can compiler suggests what to do in such cases? It would improve UX and help people who can't figure it out. Just throwing an idea, but probably cost outweighs benefits. |
Yeah, perhaps we can find where the TypeRef to Mammal is replaced by the simplified (and lossy) lub of the hard union, and make it not replace. |
simpler testcase //> using scala "3.3.1"
sealed trait Animal
object Animal {
case object Cat extends Animal
case object Dog extends Animal
}
type Mammal = Animal.Cat.type | Animal.Dog.type
val mammals: List[Mammal] = ???
val result = mammals.head
val mammal: Mammal = result // << error |
Compiler version
3.3.1
Minimized code
Output
There is a compilation error in
result3
Expectation
result3
works correctly, the same as 1 & 2, inferred type should beCat | Dog
orMammal
.The text was updated successfully, but these errors were encountered: