应用程序中的scalaz版本更新后DecodeJson不工作
我尝试将scalaz版本升级到应用程序中的scalaz版本更新后DecodeJson不工作,scala,scalaz,argonaut,Scala,Scalaz,Argonaut,我尝试将scalaz版本升级到7.2.18。在以前的版本中,下面的代码块工作得很好 implicit val decode: DecodeJson[Uuid] = DecodeJson( cursor => cursor.as[String].flatMap( str => DecodeResult( \/.fromTryCatchThrowable[Uuid,IllegalArgumentException](from
7.2.18
。在以前的版本中,下面的代码块工作得很好
implicit val decode: DecodeJson[Uuid] =
DecodeJson( cursor =>
cursor.as[String].flatMap( str =>
DecodeResult(
\/.fromTryCatchThrowable[Uuid,IllegalArgumentException](from(str))
.leftMap(exc => (exc.getMessage, cursor.history))
) ) )
但是我升级了版本,decodesult(…)
块给出了错误:
Type Mismatch,
expected: Either((String, CursorHistory), NotInferredA)
actual : \/((String, CursorHistory),Uuid)
如果有人能告诉我发生错误的原因以及上述块的正确实现,我将不胜感激。我怀疑您使用了JSON的
Argonaut
库,并且您的DecodeJson
和DecodeResult
来自此库。很难猜测它以前是如何工作的,因为您没有指定您升级的那些库的哪些版本以及您有哪些其他依赖项(即代码工作的时间)
目前的问题来自这样一个事实,decodesult
需要标准scala库中的scala.util.other
即other
,而您提供的是scalaz.\/
,这是与scalaz库中的other功能丰富的等价物。此外,这些类型是同构的(形状相同),并且可以很容易地相互转换,就编译器所知scala.util。
和scalaz.\/
是两个不相关的类。可能最简单的修复方法是使用\/.toEither
方法转换值:
implicit val decode: DecodeJson[Uuid] =
DecodeJson(cursor =>
cursor.as[String].flatMap(str =>
DecodeResult(
\/.fromTryCatchThrowable[Uuid, IllegalArgumentException](Uuid.from(str))
.leftMap(exc => (exc.getMessage, cursor.history)).toEither
)))
或者,您可以尝试查找早期的哪些依赖项带来了从\/
到或的一些自动转换。或者你可以自己写:
object ScalaZEitherHelper {
implicit def scalaZToStd[A, B](scalazValue: A \/ B): Either[A, B] = scalazValue.toEither
}
然后,您的原始代码将按照您所做的方式编译。
import scalazetherhelper.\u
第一种方法工作得完美无缺。我也会尝试第二种选择。干杯