The following code does not compile with Scala 2.13.3 but with Dotty. Is there a chance that this can be fixed in the Scala 2.13 line? Is there a workaround without giving explicit type annoations?
object TypeDeriviationTest {
trait Record
trait Table[R <: Record]
def test[R <: Record, T <: Table[R]](t: T) = ???
class Rec extends Record
class Tab extends Table[Rec]
// Error:(13, 3) inferred type arguments [Nothing,TypeDeriviationTest.Tab] do not conform to method test's type parameter bounds [R <: TypeDeriviationTest.Record,T <: TypeDeriviationTest.Table[R]]
test(new Tab)
}
You have to get the R
in the parameter list somehow:
def test[R <: Record, T <: Table[R]](t: T with Table[R]) = ???
Is there no type member trickery? In this particular case there is no record but only a table definition available.
The trick is to type t
as T with Table[R]
instead of T
.
I guess you could also make it work by making R
a type member of Table
instead of a type parameter. But whether that’s a good idea probably depends on the rest of your design and not on a single method.
Many thanks! Will apply the with
trick.
Another one: The following code does not compile with Scala 2.13.3 but with Dotty. (The LUB is not derived.) Is there a chance that this can be fixed in the Scala 2.13 line? Is there a workaround without giving explicit type annoations?
object TDT {
trait Base
class A extends Base
class B extends Base
implicit class OptMap[X](x: X) {
def optMap[O, Y >: X](o: Option[O])(f: (X, O) => Y): Y = o match {
case Some(o) => f(x, o)
case None => x
}
}
// Error:(32, 37) type mismatch;
// found : TDT.B
// required: TDT.A
(new A).optMap(Some(1))((a, o) => new B)
}