Is it possible to define methods for a case class instance? I know I can define class methods using the companion object but I’m not loving this invocation style:
Just to add to @tarsa’s answer, you can use implicits to add functionality to any class without having to change the code in the class (add hoc polymorphism). Example
case class MyCaseClass(someInt : Int)
implicit class MyCaseClassOps(instOf : MyCaseClass) {
def myFunc : Int = 1
}
val myCaseClassObj = MyCaseClass(someInt = 1)
//Now you can call the method myFunc as if it were directly defined inside the case class
myCaseClassObj.myFunc
println(s"Printing myCaseClassObj.myFunc = ${myCaseClassObj.myFunc}")