-
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 difficulties with UndefOr[A | B]
#14770
Comments
Basically it does not seem to infer sub parts of a union. Smaller example:
|
That's by design. Too much would break otherwise. We are still looking for a way to make precise type variable inference an opt-in feature. But it won't work for existing library functions such as |
I'm not sure if this is according to design actually: scala> def id[A](v: A): A = v
def id[A](v: A): A
scala> id(5: Int | Unit)
val res0: Int | Unit = 5
scala> id(5: String | Int | Unit)
val res1: String | Int | Unit = 5
scala> def test[A](v: A | Unit): A | Unit = v
def test[A](v: A | Unit): A | Unit
scala> test(5: Int | Unit)
val res2: Int | Unit = 5
scala> test(5: String | Int | Unit)
val res3: Matchable | Unit = 5 we manage to keep the hard union when instantiating the type variable of |
Polite bump on this. No promises/timelines of course, but any gut feelings whether this might be fixed/improved? Have to decide between rewriting some large facades to workaround this or deferring the Scala 3 migration for now. Thanks! |
I don't personally have the time to work on anything for the next few months. |
Right, I don't expect anyone to work on this anytime soon. More the heart of my question is, is this "by-design" as suggested in #14770 (comment)? Because if so then we should just cut our losses :) |
I don't think it's by design, we have some logic to try to preserve hard unions (that is: unions written by the user rather than inferred) through type inference already: (This might also help with other issues where we currently don't preserve unions: #14494) |
UnderOr[A | B]
UndefOr[A | B]
Compiler version
3.1.3-RC1-bin-20220323-fb7f900-NIGHTLY
Minimized code
UndefOr
is based on the Scala.js concept:https://github.com/scala-js/scala-js/blob/058532aa8c504b76431b40e3e1b51b2cdef87643/library/src/main/scala/scala/scalajs/js/package.scala#L85
The minimized example below demonstrates a common situation when working with Scala.js facades.
Output
Expectation
If we add an explicit type annotation to the last line:
then it compiles ok:
Is it possible for the compiler to make this inference by itself?
The text was updated successfully, but these errors were encountered: