6

为什么以下不起作用?

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)
}

但这遇到了与我上面较小的测试用例相同的问题。

4

2 回答 2

2

在第一次尝试中,您确实拥有Hoo[B] with B <: Foo[B]. 但是为了Goo[Hoo[B] with B]存在,你需要Hoo[B] with B <: Foo[Hoo[B] with B]. 第二种情况也是如此。

于 2011-07-27T08:30:17.233 回答
0

这似乎太简单了(即:成为一个好习惯),但这无论如何都挽救了我的一天,所以它在哪里:

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
于 2013-08-21T15:08:30.797 回答