在隐式类中创建的标量不兼容嵌套类型

问题描述 投票:5回答:2

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

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

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被视为原始注册表对象的不可比较的类型

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#Entry中的RegistryOps,但没有运气)如果我错过了一些理解并且它们实际上是不同的类型,那么我将不胜感激任何解释和示例,为什么将它们视为相同可能会破坏类型系统。谢谢!

scala inner-classes implicit scala-2.12
2个回答
4
投票

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

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成为内部案例类是因为您想避免这种事情。如果您不在乎,您真的应该只将Entry提升为自己的具有T的顶级案例类。

作为旁注,您可能会认为仅编写以下内容会起作用:

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

((作为一个脚注,我不得不说,具有某种可变的状态,您隐式地绕过听起来像是一场噩梦,我强烈建议您不要像您在这里所做的那样远程地做任何事情。)


0
投票

发布自我答复希望它可以对某人有所帮助:

我们可以使用额外的类型参数将Entry类型从Registry中移出到RegistryEntry中,并在其中将注册表自类型绑定到类型别名中,例如:

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

这将确保原始问题中要求的类型安全,并且“问题”代码段也将编译。

© www.soinside.com 2019 - 2024. All rights reserved.