尝试将 F 有界多态性建模为 Scala 中的类型成员 [英] Attempting to model F-bounded polymorphism as a type member in Scala

查看:11
本文介绍了尝试将 F 有界多态性建模为 Scala 中的类型成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to try writing a type whose methods can be homogeneous and return values of the same type:

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

Unfortunately, the commented-out line calling tester fails with the following error (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()))
      ^

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

Basically, I'm confused as to why X and Y don't unify to Foo, which seems like a clear LUB for the two of them. Clearly the type member is complicating matters but its bounds appear to be respected.

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

At the higher level, I'm looking for a lightweight way to get the equivalent of F-bounded polymorphism without the overhead of pervasive type parameters. This mostly seems to work, but I need to add annotations that force X and Y to unify to Foo.

推荐答案

我认为这是您正在寻找的示例:

I think this is an example of what you are looking for:

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
}

如果您想了解更多,此片段来自 一篇涵盖相关概念的最新帖子.

If you would like to read more, this snippet is from a recent post that covers related concepts.

要完成答案,应该是:

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

这篇关于尝试将 F 有界多态性建模为 Scala 中的类型成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆