0

Good day everyone, I create the following case class on 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
}

Then, as you can see, BGP contains Term and extends to pattern, Val contains Values and extends to Term, and U,L,B contains Strings and extends to Value, In my function I want to access to the strings that contains the U or L or B case classes, the variable valor = Val(y).value contains a U class for example, but when I write valor.XXXX don't appear me the name option. The big question is How can I do to accesss to the String name from U?

2
  • You treat your U, L, B class instance as a Value, which doesn't have any methods/fields declared but you expect it to magically have them when you want? :D Commented Apr 27, 2017 at 9:39
  • Yes, I expect the magic properties from SCALA, XD, @flavian gave me the correct answer Commented Apr 27, 2017 at 16:18

1 Answer 1

2

You just define it on Value which btw could be a 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
Sign up to request clarification or add additional context in comments.

3 Comments

This classes in this moment are under a trait: trait SPARQLInterpreter { sealed abstract class Value; case class URI(name: Node) extends Value case class Literal(name: Node) extends Value case class BlankNode(name: Node) extends Value
@VladimirRincon I don't see how that makes a difference, can you not access the Value class, is that what you are saying?
Ok @flavian, I see your point and I implemented, you are right and it works, thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.