I want to pass a function fun1 as a parameter to fun2.
However fun1 needs an implicit parameter. Is is possible to define that implicit value inside fun2?
The code is like this:
import org.json4s._
import org.json4s.jackson.JsonMethods._
def fun1(json:JValue)(implicit formats: Formats) = {
//do something
}
def fun2(f: (JValue) => RatEvent,line:String ) = {
implicit val formats = DefaultFormats //has been defined in import
val json = parse(line) //covert string to jvalue
val result = f(json)
}
Here I pass fun1 to fun2, compiler complains that it cannot find implicit value for fun1.
fun2(fun1,"asdfasdf") //error here, fun1 is lack of an implicit value
I want to solve the problem by changing the shape of fun2 i.e.
def fun2(f: (JValue)(implicit Formats) => RatEvent,line:String ) //doesn't compile
But I don't know how to write it correctly.
Supplement:
Declare an implicit Format may seems a good solution. But I have to define it in fun2. I have made problem simple. The real funs looks like:
def fun2(f: (JValue) => RatEvent,lines:RDD[String] )(implicit sc:SparkContext) = {
for (
line <- lines
){
implicit val formats = DefaultFormats //has been defined in import
val json = parse(line) //covert string to jvalue
val result = f(json)
......
}
}
formats has to be defined inside of lines's map function (I used for instead).