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

我是研究僧i 提交于 2020-08-24 08:42:06

问题


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?


回答1:


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.




回答2:


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) {



回答3:


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.



来源:https://stackoverflow.com/questions/24273617/compile-error-when-using-a-companion-object-of-a-case-class-as-a-type-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!