user12129919
user12129919

Reputation:

Match values of 2 case class

Suppose I've 2 case classes :

case class A (r: String) // with value "rr"
case class B (rr:String) // with value "ask"

how can I match the two so that

if(*value of case class B(A)* == *value of case class B.rr*) true

Is it possible in scala if yes then how?

I tried A.getClass.getName.startsWith(B.rr)

but got no output

Upvotes: 0

Views: 46

Answers (1)

francoisr
francoisr

Reputation: 4585

If I understand the problem correctly, you want to check whether B.rr has some given class name as its values (and then the r member of A is irrelevant to this). Here is a solution in that case:

val b = new B("A")
b match {
    case B(classOf[A].getSimpleName) => // we know b.rr == "A"
    case _ => // b.rr != "A"
}

You can also just use if (b.rr == classOf[A].getSimpleName) ... else ...

Upvotes: 1

Related Questions