I have a map of case class copy methods constructed using a macro (How to use scala macros to create a function object (to create a Map[String, (T) => T])), using implicit conversions to convert an input to the correct type of the copy method being called (JsValue
is a Play Framework json value)
case class Clazz(int: Int, string: String)
implicit def jsonToInt(json: JsValue): Int = json.as[Int]
implicit def jsonToStr(json: JsValue): String = json.as[String]
val copyMap: Map[String, (Clazz, JsValue) => Clazz] =
Map("int" -> (c: Clazz, json: JsValue) => c.copy(int = json),
"string" -> (c: Clazz, json: JsValue) => c.copy(string = json))
I would like to partially apply an input to a function retrieved from copyMap
, e.g.
val fun: (Clazz, JsValue) => Clazz = copyMap.get("int").get
val newFun: (Clazz) => Clazz = fun(_: Clazz, JsString("non_int_input"))
In this case the function will throw an exception when trying to convert JsString("non_int_input")
to an Int
when fully applying the function, but I'd like to get this error when partially applying the JsValue
input - fully applying the function involves retrieving a Clazz
instance from a database, and so I'd rather preempt this if the string input is invalid.
Is there a way that I can trigger / extract any implicit conversion errors in the partially applied function?