You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classD[T]:classC[S] extendsD[S]:deffoo(x: T):T= x
@main defmain():Unit=valda=newD[String]
valea=new da.C[Int]
println(ea.foo(10))
Output
-- [E007] TypeMismatchError:Main.scala:8:17---------------------------------8| println(ea.foo(10))
|^^|Found: (10:Int)
|Required:String|| longer explanation available when compiling with`-explain`1 error found
Expectation
The compiler's behavior is in fact reasonable, but it contradicts with Scala 3's language spec. In particular, in the chapter about types, a meta-function asSeemFrom is defined as follows:
We define asSeenFrom(T, C, p) where baseType(p, C) = q.C[U_1,...,U_n] as follows:
If T is a reference to the ith class type parameter of some class D:
If baseType(p, D) = r.D[W_1,...,W_m] is defined, then W_i
...
...
Applying this to the code above, to type check ea.foo(10), we compute asSeenFrom(foo(x: T):T, C, ea), which requires asSeenFrom(T, C, ea). T is a reference to the 1st type parameter of D. baseType(ea, D) = .D[Int], hence T =:= Int, and so ea.foo(10) should pass type check.