15

我想尝试编写一种类型,其方法可以是同质的并返回相同类型的值:

object SimpleTest {
  trait Foo extends Product with Serializable {
    type Self <: Foo
    def bar: Self
  }

  case class X() extends Foo {
    type Self = X
    def bar = this
  }

  case class Y() extends Foo {
    type Self = Y
    def bar = this
  }


  trait TC[A]

  implicit val tc: TC[Foo] = new TC[Foo] { }

  def tester[A: TC](x: Seq[A]) = "foo"

  // tester(Seq(X(), Y()))
}

不幸的是,注释掉的行调用tester失败并出现以下错误(Scala 2.10):

Error: could not find implicit value for evidence parameter of type
SimpleTest.TC[SimpleTest.Foo{type Self >: SimpleTest.Y with SimpleTest.X <: SimpleTest.Foo}]
tester(Seq(X(), Y()))
      ^

X基本上,我对为什么Y不统一到感到困惑Foo,这对于他们两个来说似乎是一个明确的 LUB。显然,类型成员使事情复杂化,但它的界限似乎得到了尊重。

在更高的层次上,我正在寻找一种轻量级的方法来获得等效的 F 有界多态性,而无需普遍类型参数的开销。这似乎最有效,但我需要添加强制XY统一到Foo.

4

1 回答 1

19

我认为这是您正在寻找的一个例子:

sealed trait Event { self =>
  type E >: self.type <: Event
  def instance: E = self
}

case class UserJoined() extends Event {
  type E = UserJoined
}

case class UserLeft() extends Event {
  type E = UserLeft
}

如果您想了解更多信息,此片段来自最近的一篇文章,其中涵盖了相关概念。

编辑:要完成答案,它将是:

scala> trait Foo extends Product with Serializable with Event{}
defined trait Foo

scala> case class X() extends Foo {
     |     type Self = X
     |     def bar = this
     |   }
defined class X

scala> case class Y() extends Foo {
     |     type Self = Y
     |     def bar = this
     |   }
defined class Y

scala> List(X(),Y())
res9: List[Foo] = List(X(), Y())

scala>   def tester[A: TC](x: Seq[A]) = "foo"
tester: [A](x: Seq[A])(implicit evidence$1: TC[A])String

scala>  tester(Seq(X(), Y()))
res10: String = foo
于 2014-05-05T21:42:09.107 回答