I have a case class defined as such:
case class StreetSecondary(designator: String, value: Option[String])
I then define an explicit companion object:
object StreetSecondary {
//empty for now
}
The act of defining the explicit companion object StreetSecondary causes the compiler produced "implicit companion object" to be lost; i.e. replaced with no ability to access the compiler produced version. For example, the tupled
method is available on case class StreetSecondary via this implicit companion object. However, once I define the explicit companion object, the tupled
method is "lost".
So, what do I need to define/add/change to the above StreetSecondary explicit companion object to regain all the functionality lost with the replacement of the compiler provided implicit companion object? And I want more than just the tupled
method restored. I want all functionality (for example, including extractor/unapply
) restored.
Thank you for any direction/guidance you can offer.
UPDATE 1
I have done enough searching to discover several things:
A) The explicit companion object must be defined BEFORE its case class (at least that is the case in the Eclipse Scala-IDE WorkSheet, and the code doesn't work in the IntelliJ IDE's WorkSheet regardless of which comes first).
B) There is a technical trick to force tupled
to work (thank you drstevens): (StreetSecondary.apply _).tupled
While that solves the specific tupled
method problem, it still doesn't accurately or completely describe what the scala compiler is providing in the implicit companion object.
C) Finally, the explicit companion object can be defined to extend a function which matches the signature of the parameters of the primary constructor and returns an instance of the case class. It looks like this:
object StreetSecondary extends ((String, Option[String]) => StreetSecondary) {
//empty for now
}
Again, I am still not confident accurately or completely describes what the scala compiler is providing in the implicit companion object.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…