Как я могу сделать следующую работу?
trait T {
type I <: T
def apply(i: I): I
}
class Foo[T0 <: T](t: T0) {
def bar(x: T0) = t(x)
}
implicit def tofoo[T0 <: T](t: T0) = new Foo(t)
Строка bar
выдает ошибку:
type mismatch; found : x.type (with underlying type T0) required: Foo.this.t.I
(Можно спорить, почему сутенер и bar
делаеттак же, как apply
в T
. Но это потому, что я уменьшил проблему. В моем рабочем коде у меня есть Seq [T] в качестве параметра bar
.)
РЕДАКТИРОВАТЬ:
В связи с ответом @AlexeyRomanov я показываю пример (также сокращен из рабочего кода), что также должно работать:
trait T {
type I <: T
def apply(i: I): I
}
class Foo[T0 <: T { type I = T0 }](val t: T0) {
def bar(x: T0) = t(x)
def test = "!"
}
implicit def tofoo[T0 <: T { type I = T0 }](t: T0) = new Foo(t)
trait TA extends T {
type I = TA
}
case class TB() extends TA {
def apply(i: I): I = i
}
println(TB().test) // ERROR: value test is not a member of TB