How to pass a function with an implicit parameter

I want to pass a function fun1as a parameter fun2. However fun1, an implicit parameter is needed. Is it possible to define an implicit value inside fun2?

The code is as follows:

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 fun1in fun2, the compiler complains that it cannot find the 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 form fun2ie

def fun2(f: (JValue)(implicit Formats) => RatEvent,line:String ) //doesn't compile

But I do not know how to write it down correctly.

Addition:

Declaring an implicit format might seem like a good solution. But I have to define it in fun2. I made the problem simple. Real fun looks like this:

  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)
        ......
        }
  }

formatsmust be defined inside the function lines map(I used it instead for).

+4
1

A Scala value apply . .

fun2, :

class J; class F; class R

def fun1(j: J)(implicit f: F): R =
  new R
def fun2(fn: (J, F) => R) {
  fn(new J, new F)
}

fun2(fun1(_)(_))

yuck; , fun2(fun1).

F , fun2 :

class J; class F; class R

def fun1(j: J)(implicit f: F): R =
  new R

def fun2(fn: J => R)(implicit f: F) {
  fn(new J)
}

implicit val f = new F
fun2(fun1)

Yay, !

? Formats fun2, , - , , .

+1

Source: https://habr.com/ru/post/1584388/