I have some case classes that extend this class:
class CitySuggestion(val name: String, val locationId: String, val locationKind: String)
I wanted to use Json.writes[CitySuggestion]
as a JSON writer for this class, so I defined an unapply
method in its companion object:
object CitySuggestion {
def unapply(cs: CitySuggestion): Option[(String, String, String)] =
Some((cs.name, cs.locationId, cs.locationKind))
}
I thought this would suffice, but the compiler now asks for an apply
method as well:
No apply function found
[error] implicit lazy val citySuggestionWrites = Json.writes[CitySuggestion]
Why does it need an apply method? I don't want to deserialize the JSON, and I would have to add logic to figure out which subclass's instance it should be deserialized to.
CitySuggestion
a case class you won't need to manually define either method, though your use case might prevent that. – gregghz