“理解”;类型参数不符合类型参数界限;Scala中的错误
为什么不做下面的工作“理解”;类型参数不符合类型参数界限;Scala中的错误,scala,bounded-quantification,Scala,Bounded Quantification,为什么不做下面的工作 scala> abstract class Foo[B<:Foo[B]] defined class Foo scala> class Goo[B<:Foo[B]](x: B) defined class Goo scala> trait Hoo[B<:Foo[B]] { self: B => new Goo(self) } <console>:9: error: inferred type arguments [H
scala> abstract class Foo[B<:Foo[B]]
defined class Foo
scala> class Goo[B<:Foo[B]](x: B)
defined class Goo
scala> trait Hoo[B<:Foo[B]] { self: B => new Goo(self) }
<console>:9: error: inferred type arguments [Hoo[B] with B] do not conform to class Goo's type parameter bounds [B <: Foo[B]]
trait Hoo[B<:Foo[B]] { self: B => new Goo(self) }
^
scala> trait Hoo[B<:Foo[B]] extends Foo[B] { new Goo(this) }
<console>:9: error: inferred type arguments [Hoo[B]] do not conform to class Goo's type parameter bounds [B <: Foo[B]]
trait Hoo[B<:Foo[B]] extends Foo[B] { new Goo(this) }
^
我的尝试:
class XDefinitionHelper[R <: Record[_, R]](name: String, record: R) {
def BYTEA = ...
}
trait XRecord[PK, R <: Record[PK, R]] { self: R =>
implicit def newView(x: String) = new XDefinitionHelper(x, self)
}
classxdefinitionhelper[R在第一次尝试中,您确实有Hoo[B]和B这似乎太简单了,不可能是真的(即:这是一个好的实践),但这挽救了我的一天,所以它是:
scala> trait MyTrait[T <: MyTrait[T]] { self: T => def hello = println("hello") }
scala> case class User(t: MyTrait[_])
<console>:8: error: type arguments [_$1] do not conform to trait MyTrait's type parameter bounds [T <: MyTrait[T]]
case class User(t: MyTrait[_])
scala> case class User(t: () => MyTrait[_])
defined class User
scala>trait MyTrait[T def hello=println(“hello”)}
scala>case类用户(t:MyTrait[\uz])
:8:错误:类型参数[$1]不符合trait MyTrait的类型参数界限[T case class User(T:()=>MyTrait[$1])
定义类用户
谢谢,我现在看到了如何修复我的第二次尝试(使用扩展Foo[Hoo[B]]
),但是如何修复第一个表单(使用self-type)?进一步解决这个问题也有困难。我需要定义一个类Ioo[B上面应该是:我需要定义一个类Ioo扩展Hoo[Ioo]{new Goo(this)}
但这不起作用。也不能扩展Hoo[Foo[Ioo]]
。
class XDefinitionHelper[R <: Record[_, R]](name: String, record: R) {
def BYTEA = ...
}
trait XRecord[PK, R <: Record[PK, R]] { self: R =>
implicit def newView(x: String) = new XDefinitionHelper(x, self)
}
scala> trait MyTrait[T <: MyTrait[T]] { self: T => def hello = println("hello") }
scala> case class User(t: MyTrait[_])
<console>:8: error: type arguments [_$1] do not conform to trait MyTrait's type parameter bounds [T <: MyTrait[T]]
case class User(t: MyTrait[_])
scala> case class User(t: () => MyTrait[_])
defined class User