【发布时间】:2015-11-25 13:27:37
【问题描述】:
考虑以下几点:
trait Foo {
type F[_]
type A
type FA = F[A]
def get: FA
}
class SeqStringFoo extends Foo {
type F[_] = Seq[_]
type A = String
def get: Seq[String] = Seq("hello world")
}
def exec[F <: Foo](foo: F): F#FA = foo.get
val seq1: Seq[Any] = exec(new SeqStringFoo()) // Seq[Any] = List(hello world)
val seq2: Seq[String] = exec(new SeqStringFoo()) // Error: Expression SeqIntFoo#FA doesn't conform to Seq[String]
seq2 无法编译,因为由于某种原因,包装类型 String 的类型信息在使用 type projection F#FA 时会丢失。
当返回的类型不是更高种类的类型时,不会发生这种情况。
为什么会这样?
我该如何解决这个问题?
【问题讨论】:
标签: scala higher-kinded-types type-projection