1

I would like to pass a function with signature

def insert(o: Role)(implicit s: Session): UUID

into a function (as inserter) with signature

def insertRows[T](instanceList: List[T], inserter: T => UUID): Unit

How do I specify that inserter has an implicit Session?

ic3b3rg
  • 14,629
  • 4
  • 30
  • 53

2 Answers2

1

First you should find out how to make a function from method, that requires implicit:

scala> def a(a: Int)(implicit b: Int) = a
a: (a: Int)(implicit b: Int)Int

scala> a _
<console>:9: error: could not find implicit value for parameter b: Int
          a _
          ^ 
//I assume you can't specify implicit before `a _`, otherwise you have your answer anyway :)

scala> a(_: Int)(_: Int)
res18: (Int, Int) => Int = <function2>

Then, it becomes clear what to pass:

scala> def f(f: (Int, Int) => Int) =0
f: (f: (Int, Int) => Int)Int

scala> f(a(_: Int)(_: Int))
res16: Int = 0

Or even:

scala> f(a(_)(_))
res25: Int = 0

For any other curried function this works. I hope one day scala will became smart enough to support same way for implicits.

P.S. In your specific case:

 def insertRows[T](instanceList: List[T], inserter: (T, Session) => UUID): Unit

 insertRows[Role](list, insert(_)(_))
Community
  • 1
  • 1
dk14
  • 22,206
  • 4
  • 51
  • 88
0

You don't need to specify that insert uses an implicit. Only requirement is that there should be an implicit value available while using it.

implicit val s = new Session(...) // or import if defined elsewhere.
insertRows(list, insert)
Shyamendra Solanki
  • 8,751
  • 2
  • 31
  • 25