Having trouble with implicit conversion in scala

限于喜欢 提交于 2019-12-25 03:22:42

问题


Having this code

case class Workspace(ident: Long, name: String)
case class Project(ident: Long, name: String)

implicit def workspaceJSON: JSONR[Workspace] = new JSONR[Workspace] {
  def read(json: JValue) =
    Workspace.applyJSON(field[Long]("id"), field[String]("name"))(json)
}

implicit def projectJSON: JSONR[Project] = new JSONR[Project] {
  def read(json: JValue) =
    Project.applyJSON(field[Long]("id"), field[String]("name"))(json)
}

def parseEnt[T: JSONR](json: JValue): Either[String, T] =
  fromJSON[T](json).toEither.left.map{ _.toString }

def fetchProjects(ws: Workspace): Either[String, Project] = {
  parseEnt(parse("some text"))
}

Which fails to compile on parseEnt(parse("some text")) with

ambiguous implicit values:  both method taskJSON in class Fetcher of type =>
Fetcher.this.JSONR[types.Task]  and method workspaceJSON in class Fetcher of type =>
Fetcher.this.JSONR[Fetcher.this.Workspace]  match expected type Fetcher.this.JSONR[T]

Is there a way to assure scala, that in this case I want type variable T to be a Project and choose projectJSON function to parse it? Or if I'm doing it wrong, then how do it in right way?


回答1:


The compiler is trying to automatically infer the type T, which must be something that it can be produced starting from a String (more or less, but details an unimportant here)

Unfortunately it can't succeed, since you're providing multiple implicit conversions going from String to Project and Workspace. The simple solution is to explicitly indicate the type you're trying to produce:

parseEnt[Project](parse("some text"))

This pattern is fairly common with serialization type classes, where you are mapping multiple specific types to a generic one (String in this case).



来源:https://stackoverflow.com/questions/24841924/having-trouble-with-implicit-conversion-in-scala

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