Open
Description
When having an existentially parameterized value val foo: Foo[_]
, I would expect the type argument, although unknown, be stable among all usages of foo
. This is clearly not the case:
class Foo[A]
object Main {
val foo: Foo[_] = new Foo[String]
// Test 1
def f[A](foo1: Foo[A], foo2: Foo[A]): Unit = ()
f(foo, foo) // error
// Test 2
case class Bar[A](foo: Foo[A]) {
def f(foo: Foo[A]): Unit = ()
}
Bar(foo).f(foo) // error
}
This is the compiler output:
$ ~/scala-2.12.2/bin/scalac main.scala
main.scala:8: error: type mismatch;
found : Foo[_$1] where type _$1
required: Foo[Any]
Note: _$1 <: Any, but class Foo is invariant in type A.
You may wish to define A as +A instead. (SLS 4.5)
f(foo, foo) // error
^
main.scala:8: error: type mismatch;
found : Foo[_$1] where type _$1
required: Foo[Any]
Note: _$1 <: Any, but class Foo is invariant in type A.
You may wish to define A as +A instead. (SLS 4.5)
f(foo, foo) // error
^
main.scala:14: error: type mismatch;
found : Foo[(some other)_$1(in value <local Main>)] where type (some other)_$1(in value <local Main>)
required: Foo[_$1(in value <local Main>)] where type _$1(in value <local Main>)
Bar(foo).f(foo) // error
^
three errors found