Scala多个嵌套案例条款

时间:2017-04-26 23:47:13

标签: scala case-class

大家好,我在SCALA上创建了以下案例类:

sealed abstract class Value;
  case class U(name: String) extends Value
  case class L(name: String) extends Value
  case class B(name: String) extends Value

  sealed abstract class Term
  case class Var(name: String) extends Term //variable name
  case class Val(value: Value) extends Term //value

sealed abstract class Pattern //patterns
  case class BGP(subject: Term, predicate: Term, obj: Term) extends Pattern
  case class And( pat1: Pattern, pat2: Pattern) extends Pattern
  case class Filter(pred: Predicate, pattern: Pattern ) extends Pattern


def function(p: Pattern): Unit = p match { 
    case BGP(Var(x), Val(y), Val(z)) => {
      val con:conv = new conv()
      val valor:Value = Val(y).value
}

然后,正如您所看到的,BGP包含Term并扩展到pattern,Val包含Values并扩展到Term,而U,L,B包含Strings并扩展为Value, 在我的函数中我想访问包含U或L或B案例类的字符串,变量valor = Val(y).value包含一个U类,例如,当我写valor.XXXX时不会出现我名称选项。最大的问题是如何从U访问String名称?

1 个答案:

答案 0 :(得分:2)

您只需在Value上定义,即btw可能是trait

sealed trait Value {
  def name: String
}
case class U(name: String) extends Value
case class L(name: String) extends Value
case class B(name: String) extends Value