3

我在尝试为通用案例类实现 JsonFormat 对象时遇到了这个问题。这是我的课:

case class SimpleQuery[T](field : String, op : Operator, value : T) extends Query{
  def getType = ????
}

我正在尝试使用喷雾 json 的 github 页面提示的格式,如下所示:

implicit def SimpleQueryJsonFormat[A <: JsonFormat] = jsonFormat4(SimpleQuery.apply[A])

但我得到这个编译器错误

trait JsonFormat takes type parameters

spray-json github 页面的示例如下:

case class NamedList[A](name: String, items: List[A])

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit def namedListFormat[A :JsonFormat] = jsonFormat2(NamedList.apply[A])
}

这似乎和我的很相似。

我还将在 github 页面中打开一个问题。

先感谢您

4

1 回答 1

0

我认为您可能会混淆定义的类型参数中的 the<:和 the :

在你的,[A <: JsonFormat]意思是“A扩展JsonFormat”。

在示例中,[A :JsonFormat]表示“A带有隐含JsonFormat[A]”。这与需要隐式(但未命名)参数相同,例如implicit aFormat: JsonFormat[A]. 这是格式化课程的value: T一部分所必需的。

TL;DR,尝试切换<::

于 2015-03-27T02:52:50.120 回答