または、言い換えると、フィールド (引数) の値が異なるにもかかわらず、タプル内の要素が同じケース クラスであるかどうかを照合して検証できますか? 以下のケース[T]に相当するものはありますか?
sealed abstract class RootClass
case class ChildClassX(valuex: Boolean) extends RootClass
case class ChildClassY(valuey: Boolean) extends RootClass
// and other case classes here...
object Foo {
def compare(a: RootClass, b: RootClass) = {
(a, b) match {
case[T] (T(a), T(b)) => a == b
case _ => throw Exception("a and b should be of same child classes.")
}
}
私がする必要がないことを願っています:
object Foo {
def compare(a: RootClass, b: RootClass) = {
(a, b) match {
case (ChildClassX(a), ChildClassX(b)) | (ChildClassY(a), ChildClassY(b)) | (ChildClassZ(a), ChildClassZ(b)) | etc. => a == b
case _ => throw Exception("a and b should be of same child classes.")
}
}
関連: マッチング