jonderry
jonderry

Reputation: 23633

Compile error when using a companion object of a case class as a type parameter

I'm create a number of json messages for spray in scala using case classes. For example:

  case class Foo(name: String, attrs: List[String])
  implicit val fooFormat = jsonFormat2(Foo)
  object Foo {
    case class Invalid(error: String)
  }
  case class Bar(name: String, kv: Map[String, String])
  implicit val barFormat = jsonFormat2(Bar)

In the above snippet, barFormat compiles, but fooFormat does not:

type mismatch; found : Foo.type required: (?, ?) => ? 
 Note: implicit value barFormat is not applicable here because it comes 
 after the application point and it lacks an explicit result type

I don't want to use barFormat in place of fooFormat, and I understand that a case class automatically generates a companion object, but I don't understand why there's a compiler error here, and the error message is difficult for me to decipher. Does anyone know what the problem is here and how to fix it, preferably without removing my Foo companion object?

Upvotes: 6

Views: 3539

Answers (3)

akauppi
akauppi

Reputation: 18036

Spray.json documentation suggests an easier way than @wingedsubmariner's suggestion:

If you explicitly declare the companion object for your case class the notation above will stop working. You'll have to explicitly refer to the companion object's apply method to fix this:

So the correction for the question becomes:

implicit val fooFormat = jsonFormat2(Foo.apply)

Added: This is in fact what also @geoffliu suggests in his answer.

Upvotes: 1

wingedsubmariner
wingedsubmariner

Reputation: 13667

Case class companion objects will by default extend one of the function traits. object Foo would have extended ((String, List[String]) => Foo) but when you manually defined it you didn't extend that trait. This is why you couldn't pass it to jsonFormat2, which was expecting a (?, ?) => ?. If you make the following change your code should compile:

object Foo extends ((String, List[String]) => Foo) {

Upvotes: 4

geoffliu
geoffliu

Reputation: 520

From your compile error, it looks like jsonFormat2 expects a two-argument function. Do you mean to pass the constructors of Foo and Bar into it? If so, you should do Foo.apply and Bar.apply.

Upvotes: 9

Related Questions