You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current implementation of CaseClass.construct means that typeclasses that stop at Functor can be implemented (e.g. Default), but typeclasses with a MonadError (e.g. a JSON decoder) cannot.
The workaround is to use locally scoped exceptions, e.g.:
privatecaseclassFail(msg: String) extendsExceptionwithNoStackTracedefcombine[A](ctx: CaseClass[JsDecoder, A]):JsDecoder[A] = {
case obj @JsObject(_) =>try {
valsuccess= ctx.construct { p =>valvalue= obj.get(p.label).getOrElse(JsNull)
p.typeclass.fromJson(value) match {
case \/-(got) => got
case-\/(fail) =>thrownewFail(fail) // scalafix:ok
}
}
\/-(success)
} catch {
caseFail(msg) =>-\/(msg)
}
case other =>JsDecoder.fail("JsObject", other)
}
it would be better if you allowed a traversal, even just for Either, allowing
privatecaseclassFail(msg: String) extendsExceptionwithNoStackTracedefcombine[A](ctx: CaseClass[JsDecoder, A]):JsDecoder[A] = {
case obj @JsObject(_) =>
ctx.constructEither { p =>valvalue= obj.get(p.label).getOrElse(JsNull)
p.typeclass.fromJson(value)
}
case other =>JsDecoder.fail("JsObject", other)
}
If you produced the boilerplate that called .flatMap then users could even use their own data types, not just Either.
The text was updated successfully, but these errors were encountered:
The current implementation of
CaseClass.construct
means that typeclasses that stop atFunctor
can be implemented (e.g.Default
), but typeclasses with aMonadError
(e.g. a JSON decoder) cannot.The workaround is to use locally scoped exceptions, e.g.:
it would be better if you allowed a traversal, even just for
Either
, allowingIf you produced the boilerplate that called
.flatMap
then users could even use their own data types, not justEither
.The text was updated successfully, but these errors were encountered: