다음 작업을 수행하지 않는 이유는 무엇입니까?스칼라의 "형식 인수가 형식 매개 변수 경계를 준수하지 않음"오류 이해
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) }
^
처음 시도 할 때 Hoo[B] with B <: Foo[B]
이 아닙니까?
두 번째 시도에서 Hoo[B] <: Foo[B]
이 아닌가요?
// "Foo"
abstract class Record[PK, R <: Record[PK, R]] extends Equals { this: R =>
implicit def view(x: String) = new DefinitionHelper(x, this)
...
}
// "Hoo"
class DefinitionHelper[R <: Record[_, R]](name: String, record: R) {
def TEXT = ...
...
}
// now you can write:
class MyRecord extends Record[Int, MyRecord] {
val myfield = "myfield".TEXT
}
하나가 쓸 수 있도록 내가, TEXT라는 BYTEA 함께 새로운 확장 방법을 소개하려고 해요 :
이 문제를 동기를 부여하려면, 도서관 거기
class MyRecord extends XRecord[Int, MyRecord] {
val myfield = "myfield".BYTEA // implicit active only inside this scope
}
내 시도 :
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)
}
위의 작은 테스트 케이스와 동일한 문제가 발생합니다.
덕분에, 지금 둘째를 해결하는 방법 참조 ('extends Foo [Hoo [B]]'를 사용하여) 시도하지만, 첫 번째 형태를 어떻게 고정 할 것인가? – Yang
더 이상 복용하는 데 문제가 있습니다. 'class Ioo [B <: Ioo [B]]는 Hoo [B] {new Goo (this)}를 확장 할 필요가 있지만 작동하지 않습니다. 'Hoo [Ioo [B]]를 확장하지도 않는다. 그러나'클래스 Ioo (Boo : Boo : Boo)는'Hoo'에서'Ioo'의 인스턴스에'Goo'라고 부른다. 그러나 나는 '그'안에서 그렇게 할 수 없다. – Yang
위의 내용은 다음과 같아야합니다.'class Ioo extends Hoo [Ioo] {new Goo (this)}'를 정의해야하지만 작동하지 않습니다. 'Hoo [Foo [Ioo]]를 확장하지도 않습니다. – Yang