具有类型成员的案例对象的模式匹配

Pattern Match on Case Objects with Type Members

Scala 有一个很好的功能可以在模式匹配中推断类型参数。它还检查模式匹配的详尽性。例如:

sealed trait PField[T]

case object PField1 extends PField[String]

case object PField2 extends PField[Int]

def getValue[X](f: PField[X]): X = f match {
  case PField1 => "aaa"
  case PField2 => 123
}

是否可以实现相同但使用类型成员而不是类型参数?

sealed trait Field {
  type T
}

case object Field1 extends Field {
  type T = String
}

case object Field2 extends Field {
  type T = Int
}

以下解决方案不起作用(在 Scala 2.12.6 中测试):

//No exhaustiveness check
def getValue[X](f: Field {type T = X}): X = f match {
  case Field1 => "aaa"
  //    case Field2 => 123
}

//casting needed
def getValue2[X](f: Field {type T = X}): X = (f: Field) match {
  case Field1 => "aaa".asInstanceOf[X]
  case Field2 => 123.asInstanceOf[X]
}

type Generified[X] = Field {type T = X}

//No exhaustiveness check
def getValue3[X](f: Generified[X]): X = f match {
  case Field1 => "aaa"
  //    case Field2 => 123
}

类型参数在我的情况下确实有问题,因为我有许多字段的子层次结构,每个层次结构都有某种类型 类。我不能将所有需要的依赖项放在案例对象中,因为它们是在一个瘦 JAR 中导出到客户端的。

所有扩展 Field 的案例都是单例对象,因此对于 Field 的每个子类型 F 它包含:

 if
    a: F, b: F, b.T = X
 then
    a.T = X

这通常不成立,例如

class Bar { type Q }
case class Field3(b: Bar) extends Field { type T = b.Q }

认为 a.T = b.T 对于 Field3.

类型的任意两个值 a, b

所以,你必须以某种方式保证 Field 的所有子 类 都像 Field1Field2 一样表现良好,并且它们不像假设的那样Field3。您可以通过向 getValue 添加一个隐式参数来执行此操作,该参数充当该字段行为良好的证明。例如,证明该字段确实是一个单例对象就足够了。

这是一个粗略的草图:

sealed trait Field { type T }
case object Field1 extends Field { type T = String }
case object Field2 extends Field { type T = Int }

sealed trait UniqGuarantee[UniqueTypeAsPathDependent]
case class S1UG[X](f: String =:= X) extends UniqGuarantee[X]
case class S2UG[X](f: Int =:= X) extends UniqGuarantee[X]

sealed trait IsSingleton[F <: Field] {
  def apply(singleton: F): UniqGuarantee[singleton.T]
}

implicit object F1_is_Singleton extends IsSingleton[Field1.type] {
  def apply(singleton: Field1.type): UniqGuarantee[singleton.T] = 
    S1UG(implicitly)
}

implicit object F2_is_Singleton extends IsSingleton[Field2.type] {
  def apply(singleton: Field2.type): UniqGuarantee[singleton.T] =
    S2UG(implicitly)
}

def getValue[F <: Field]
  (f: F)
  (implicit sing: IsSingleton[F])
: f.T = sing(f) match {
  case S1UG(uniqGuarantee) => uniqGuarantee("abc")
  case S2UG(uniqGuarantee) => uniqGuarantee(123)
}

此实现会进行类型检查,如果模式匹配不详尽,它也会显示警告。

不可否认,该解决方案非常重量级,因为它要求您实现一个完整的单独层次结构的案例 类 和充当 "proofs" 的隐式对象,您的 Field 是确实单身

我认为解决方案可以缩短很多,只是我现在不知道如何缩短。

此解决方案是@Andrey Tyukin 发布的解决方案的简化版本。 他表示

it does not hold that a.T = b.T for any two values a, b of type Field3.

这意味着,要进行详尽的模式匹配,必须忽略类型成员。因此,为了同时具有详尽性和类型推断,我们需要带有类型参数的密封层次结构。

他建议创建单独的案例层次结构 类 和模式匹配,而不是在主层次结构上。然而,在我的例子中,它可以被简化:我创建了带有类型参数的新密封特征,但相同的 case 对象用于模式匹配("unique guarantee" 保存在对象本身中)。这是最终解决方案:

sealed trait Field {
  type T
  def ug: TField[T]
}

sealed trait TField[G] extends Field {
  type T = G
  def ug: TField[T] = this
}

case object Field1 extends TField[String]
case object Field2 extends TField[Int]

def getValue[X](f: Field {type T = X}): X = (f.ug: TField[X]) match {
  case Field1 => "abc"
  case Field2 => 123
}

多亏了它,我可以使用 Field 特征来定义类型 类,而无需进入更高种类的类型,并切换到 TField[G] 进行模式匹配。