有了這個代碼經與隱式轉換的麻煩斯卡拉
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"))
}
從而未能在parseEnt(parse("some text"))
編譯
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]
有沒有一種方法,以確保階,在這種情況下,我想類型變量T
將成爲Project
並選擇projectJSON
函數來解析它?或者如果我做錯了,那麼它是如何以正確的方式呢?
如果您明確說出您期望解析的內容,那麼該怎麼辦? parseEnt [Project](解析(「一些文本」)) – johanandren
謝謝你,幫助。 – cvb