【问题标题】:Scala incompatible nested types created in implicit class在隐式类中创建的 Scala 不兼容的嵌套类型
【发布时间】:2019-02-04 09:30:45
【问题描述】:

提供的代码片段是一个虚构的简约示例,仅用于演示问题,与实际业务逻辑类型无关。

在下面的代码中,我们在Registry 类型中嵌套了一个Entry 类型。

class Registry[T](name: String) {
  case class Entry(id: Long, value: T)
}

这是有道理的,因为不同注册表的条目是不同的、无与伦比的类型。

然后我们可能有一个隐式的 Ops 类,例如,在测试中使用,它将我们的注册表绑定到一些测试存储实现,一个简单的可变映射

object testOps {

  import scala.collection.mutable

  type TestStorage[T] = mutable.Map[Long, T]

  implicit class RegistryOps[T](val self: Registry[T])(
    implicit storage: TestStorage[T]
  ) {

    def getById(id: Long): Option[self.Entry] = 
      storage.get(id).map(self.Entry(id, _))

    def remove(entry: self.Entry): Unit       = storage - entry.id
  }
}

问题是:在 Ops 包装器内部构造的 Entry 被视为与原始 Registry 对象无法比拟的类型

object problem {

  case class Item(name: String)

  val items = new Registry[Item]("elems")

  import testOps._

  implicit val storage: TestStorage[Item] = 
    scala.collection.mutable.Map[Long, Item](
      1L -> Item("whatever")
    )
  /** Compilation error: 
   found   : _1.self.Entry where val _1: testOps.RegistryOps[problem.Item]
   required: eta$0$1.self.Entry
  */
  items.getById(1).foreach(items.remove)
}

问题是: 有没有办法声明 Ops 签名以使编译器了解我们正在使用相同的内部类型? ( 我也试过 self.type#EntryRegistryOps 没有运气) 如果我错过了一些理解并且它们实际上是不同的类型,我将不胜感激任何解释和示例,为什么将它们视为相同可能会破坏类型系统。谢谢!

【问题讨论】:

  • @jwvh 这将允许您混合和匹配来自不同注册表的条目,只要它们具有相同的 T,这会违背让 Entry 成为嵌套案例类的目的全部。

标签: scala inner-classes implicit scala-2.12


【解决方案1】:

首先,值得注意的是,这里的隐含并不是真正的问题——如果你写出如下内容,它会以完全相同的方式失败:

new RegistryOps(items).getById(1).foreach(e => new RegistryOps(items).remove(e))

有很多方法可以做你想做的事情,但它们并不令人愉快。一种是对隐式类进行脱糖,以便您可以让它为注册表值捕获更具体的类型:

class Registry[T](name: String) {
  case class Entry(id: Long, value: T)
}

object testOps {
  import scala.collection.mutable

  type TestStorage[T] = mutable.Map[Long, T]

  class RegistryOps[T, R <: Registry[T]](val self: R)(
    implicit storage: TestStorage[T]
  ) {
    def getById(id: Long): Option[R#Entry] = 
      storage.get(id).map(self.Entry(id, _))

    def remove(entry: R#Entry): Unit = storage - entry.id
  }

  implicit def toRegistryOps[T](s: Registry[T])(
    implicit storage: TestStorage[T]
  ): RegistryOps[T, s.type] = new RegistryOps[T, s.type](s)
}

这工作得很好,无论是你使用它的形式,还是稍微更明确一点:

scala> import testOps._
import testOps._

scala> case class Item(name: String)
defined class Item

scala> val items = new Registry[Item]("elems")
items: Registry[Item] = Registry@69c1ea07

scala> implicit val storage: TestStorage[Item] = 
     |     scala.collection.mutable.Map[Long, Item](
     |       1L -> Item("whatever")
     |     )
storage: testOps.TestStorage[Item] = Map(1 -> Item(whatever))

scala> val resultFor1 = items.getById(1)
resultFor1: Option[items.Entry] = Some(Entry(1,Item(whatever)))

scala> resultFor1.foreach(items.remove)

请注意,resultFor1 的推断静态类型正是您所期望和想要的。与上述评论中提出的Registry[T]#Entry 解决方案不同,此方法将禁止您从一个注册表中获取条目并使用相同的T 从另一个注册表中删除它。大概你把 Entry 做成了一个内部案例类,因为你想避免这种事情。如果您不在乎,您真的应该使用自己的 TEntry 提升为自己的顶级案例类。

作为旁注,您可能认为只需编写以下内容即可:

implicit class RegistryOps[T, R <: Registry[T]](val self: R)(
  implicit storage: TestStorage[T]
) {
  def getById(id: Long): Option[R#Entry] = 
    storage.get(id).map(self.Entry(id, _))

  def remove(entry: R#Entry): Unit = storage - entry.id
}

但是你错了,因为编译器在对隐式类进行去糖时生成的合成隐式转换方法将使用比你需要的更宽的R,你会回到你没有的相同情况R。所以你必须自己写toRegistryOps并指定s.type

(作为脚注,我不得不说,你隐含地传递一些可变状态听起来像是一场绝对的噩梦,我强烈建议不要像你在这里所做的那样远程做任何事情。)

【讨论】:

  • 非常感谢!完全同意可变隐式 - 在实际代码中没有相似之处,只是一种“测试用例范围”DSL
【解决方案2】:

发布一个自我回答,希望它可以帮助某人:

我们可以将 Entry 类型从 Registry 移到带有额外类型参数的 RegistryEntry 中,并在那里在类型别名中绑定一个 Registry 自类型,例如:

case class RegistryEntry[T, R <: Registry[T]](id: Long, value: T)

case class Registry[T](name: String) {
  type Entry = RegistryEntry[T, this.type]
  def Entry(id: Long, value: T): Entry = RegistryEntry(id, value)
}

这将保证原始问题中要求的类型安全,并且“问题”代码 sn-p 也可以编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多