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 is suprisingly inferred as Any due to its kind-polymorphism #9248
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9248?orig=1 |
@retronym said (edited on Mar 25, 2015 2:46:35 AM UTC): object Test {
trait Binary[A, B]
type Unary[A] = Binary[A, A]
def f[F[A], A](f: F[A]) = ???
def test1(u: Unary[Any]) = f(u)
def test2(u: Binary[Any, Any]) = f(u) // reports inference error, cannot unifiy Binary[Any, Any] with ?F[?A]
def test3 = {
implicit def b2u[A, B](b: Binary[A, B]): List[Int] = ???
val b: Binary[Any, Any] = null
f(b) // inference fails initially, but then we try to coerse the arguments
//
// Under -Ytyper-debug, we see:
//
// searching for adaptation to pt=Test.Binary[Any,Any] => ?F[?A] (silent: method test3 in Test) implicits disabled
// | | | | 5 eligible for pt=Test.Binary[Any,Any] => ?F[?A] at (silent: method test3 in Test) implicits disabled
// | | | | [search #5] considering b2u
// | | | | |-- b2u BYVALmode-EXPRmode-FUNmode-POLYmode (silent: method test3 in Test) implicits disabled
// | | | | | [adapt] [A, B](b: Test.Binary[A,B])List[Int] adapted to [A, B](b: Test.Binary[A,B])List[Int]
// | | | | | \-> (b: Test.Binary[A,B])List[Int]
// | | | | solving for (A: ?A, B: ?B)
// | | | | [adapt] [A, B](b: Test.Binary[A,B])List[Int] adapted to [A, B](b: Test.Binary[A,B])List[Int] based on pt Test.Binary[Any,Any] => ?F[?A]
// | | | | [search #5] success inferred value of type Test.Binary[Any,Any] => ?F[?A] is SearchResult(b2u[Any, Any], )
//
// This leads to inference of ?F=Any, ?A=Nothing (this is kind-correct because Any/Nothing are kind polymorphic)
//
// When we instantatiate the method type of `f` with this, the formal parameter type is now just Any[Nothing]
// which is just Any.
//
// The provided argument of type `Binary[A, B]` now unifies with this, no implicit coercion required.
}
}
The trouble comes from the fact that we don't have a way to mark an implicit conversion for use only as a provider of extension methods. So in Scalaz, a bunch of the ToXxxOps conversions end up compromising kind checking. |
@retronym said: |
Chris Neveu (ChrisNeveu) said (edited on Mar 26, 2015 12:31:02 PM UTC): |
@adriaanm said: |
Nowadays the original example is working in Scala 2.13 and in Scala 2.12 with |
In the following example
Adding a type annotation after the map (using either the type alias or the expanded form) causes the compiler to work correctly.
The Build.scala for this is
and I have created a Github Repo illustrating the problem here
The text was updated successfully, but these errors were encountered: