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
Scala type inference fails in obvious cases #3293
Comments
Imported From: https://issues.scala-lang.org/browse/SI-3293?orig=1 |
@pchiusano said: scala> def foo[A,B,C](f: A => B, g: B => C): A => C = error("todo")
foo: [A,B,C](f: (A) => B,g: (B) => C)(A) => C
scala> foo((a: Int) => a.toDouble, _ + 2.0)
<console>:6: error: missing parameter type for expanded function ((x$$1) => x$$1.$$plus(2.0))
foo((a: Int) => a.toDouble, _ + 2.0)
^
scala> foo((a: Int) => a.toDouble, b => b + 2.0)
<console>:6: error: missing parameter type
foo((a: Int) => a.toDouble, b => b + 2.0)
^ |
@dubochet said: In theory, it would be possible to do better inference by creating a complex set of type constraints encompassing simultaneously the knowledge about the parameters and the method. However, this is extremely complex to implement. Because of this complexity, the Scala type inference system will continue to make the above simplification, for the time being. |
@pchiusano said: If there is something fundamental that I'm missing, please let me know. :) |
@retronym said: scala> def foo[A,B,C](f: A => B)(g: B => C): A => C = null scala> foo((a: Int) => a.toDouble)(_ + 2.0) |
@dubochet said: If you want to discuss the limitations of the Scala inference mechanism, I would recommend you use the mailing list, where you will get more feedback from a wider audience. |
Is there any reason why Scala cannot infer this? It seems that the type of an argument to a polymorphic function is NOT used to fill in types for the type parameters, leading to unnecessary type annotation for other arguments.
scala> def foo[A,B,C](f: A => B, g: B => C): A => C = error("todo")
foo: [A,B,C](f: (A) => B,g: (B) => C)(A) => C
scala> foo((a: Int) => a.toDouble, _ + 2.0)
:6: error: missing parameter type for expanded function ((x$$1) => x$$1.$$plus(2.0))
foo((a: Int) => a.toDouble, _ + 2.0)
^
scala> foo((a: Int) => a.toDouble, b => b + 2.0)
:6: error: missing parameter type
foo((a: Int) => a.toDouble, b => b + 2.0)
Obviously, the compiler knows that the argument f is a function from Int to Double, from which it can infer that the type parameters A and B are Int, and Double, respectively, from which it can infer that g accepts a Double, etc...
This might be better classified as an enhancement, depending on your point of view. :) This bug is probably responsible for like 85% of manual type annotations we have to insert into our code.
This was tested in 2.8.
The text was updated successfully, but these errors were encountered: