Is there a way in Scala to reference a Companion object from an instance of the Case class?

In my specific case, I have a (growing) case class library with a base attribute ( TKModel)

Then I have an abstract class ( TKModelFactory[T <: TKModel]) that extends with all related objects.

Thus, my companion objects still know the type ("T") of the "answers" that they need to provide, as well as the type of objects that they "usually" accept for common methods. (If I get lazy and cut and paste pieces of code to search for and destroy it, save my bacon a lot!) I do see warnings on the Internet, however any form CompanionObject.method(caseClassInstance: CaseClass)abounds with a “code smell”. Not sure if they really apply to Scala or not?

There seems to be no way to declare anything in the abstract case ( TKModel) class that will reference (at runtime) the corresponding companion object for a particular instance of the case class. This leads to the fact that I have to write (and edit) several method calls that I want standard for each case class.

case class Track(id: Long, name: String, statusID: Long) extends TKModel

object Track extends TKModelFactory[Track]

How can I write something TKModelso that I new Track(1, "x", 1).someMethod()can actually callTrack.objectMethod()

Yes, I can write val CO = MyCompanionObjectalong with something like implicit val CO: ???in an abstract class TKModeland make all calls dependent on this value. Trying to find some spell that makes the compiler happy for this seems like a mission is impossible. And since I cannot declare that I cannot refer to it in placeholder methods in an abstract class.

?

, ( ), , case , ?

?

+4
2

, , case , -.

case class Data(value: Int) {
  def add(data: Data) = Data.add(this,data)
}

object Data {
  def add(d1: Data, d2: Data): Data =  Data(d1.value + d2.value)
}
0

. . , , , , . , .

0

All Articles