Closed namin closed 10 years ago
More higher-kinded type examples that compile with Scala 2.10.3, but not Dotty.
import scala.language.higherKinds
trait Test {
trait Monad[X] {
def x: X
}
sealed abstract class Either[A,B]
case class Left[A,B](x: A) extends Either[A,B] with Monad[A]
case class Right[A,B](x: B) extends Either[A,B] with Monad[B]
def flatMap[X,Y,M[X]<:Monad[X]](m: M[X], f: X => M[Y]): M[Y] = f(m.x)
println(flatMap(Left(1), {x: Int => Left(x)}))
}
error: type mismatch:
found : => _$hk$0(M[X]#x)
required: X
def flatMap[X,Y,M[X]<:Monad[X]](m: M[X], f: X => M[Y]): M[Y] = f(m.x)
^
error: type mismatch:
found : Test.this.Left[Nothing', Nothing']
required: Nothing'{_$hk$0 = Nothing'}
println(flatMap(Left(1), {x: Int => Left(x)}))
^
error: type mismatch:
found : Test.this.Left[Nothing', Nothing']
required: Nothing'{_$hk$0 = Y?}
println(flatMap(Left(1), {x: Int => Left(x)}))
^
three errors found
Similar, with M
not upper bounded and flatMap
not implemented:
trait Test {
trait Monad[X] {
def x: X
}
sealed abstract class Either[A,B]
case class Left[A,B](x: A) extends Either[A,B] with Monad[A]
case class Right[A,B](x: B) extends Either[A,B] with Monad[B]
def flatMap[X,Y,M[X]](m: M[X], f: X => M[Y]): M[Y]
println(flatMap(Left(1), {x: Int => Left(x)}))
}
error: type mismatch:
found : Test.this.Left[Nothing', Nothing']
required: Nothing'{_$hk$0 = Nothing'}
println(flatMap(Left(1), {x: Int => Left(x)}))
^
found : Test.this.Left[Nothing', Nothing']
required: Nothing'{_$hk$0 = Y?}
println(flatMap(Left(1), {x: Int => Left(x)}))
^
two errors found
This one actually works in Dotty with final val flagDeepSubTypeRecursions = false
in Config
(if true, the assertion in TypeComparer.monitoredSubtype
fails.
trait Test {
def flatMap[X,Y,M[X]](m: M[X], f: X => M[Y]): M[Y]
println(flatMap(Some(1), {x: Int => Some(x)}))
}
This compiles without error:
The next trait compiles with an error (which feels inadequate and the source location is lost during reporting):
The next trait compiles with the same error (though I guess it is by design here and the source location is preserved):
All these traits compile fine with Scala 2.10.3.