Search code examples
scalacase-class

Internal case class in scala


I have the following case class

case class MyClass (LeftHandSide: (Set[String], String), RightHandSide: Double)

so, I can do the following

MyClass((Set("yu", "ye"), "bee"), 0.03).filter( x=> x.RightHandSide>4)

and I would like to able to call parts of the LeftHandSide by name too, e.g:

case class MyClass (LeftHandSide: (Part1: Set[String], Part2: String), RightHandSide: Double)

And then:

MyClass((Set("yu", "ye"), "bee"), 0.03).filter(x => x.LeftHandSide.Part2 != "bee")

Solution

  • Create an additional case class called LeftHandSide:

    case class LeftHandSide(partOne: Set[String], partTwo: String)
    

    And use that in MyClass:

    case class MyClass(leftHandSide: LeftHandSide, rightHandSide: Double)
    

    And then:

    val myClass = MyClass(LeftHandSide(Set("yu", "ye"), "bee"), 0.03)
    myClass.leftHandSide.partTwo != "bee"