Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially applying a function that has an implicit parameter

Can I turn a method which takes an implicit parameter into a function?

trait Tx

def foo(bar: Any)(implicit tx: Tx) {}

foo _ // error: could not find implicit value for parameter tx: Tx

I am trying to achieve the following, preferably if I can somehow make it work with the plain call withSelection(deleteObjects):

trait Test {      
  def atomic[A](fun: Tx => A): A

  def selection: Iterable[Any]

  def withSelection(fun: Iterable[Any] => Tx => Unit) {
    val sel = selection
    if (sel.nonEmpty) atomic { implicit tx =>
      fun(sel)(tx)
    }
  }

  object deleteAction {
    def apply() {
      withSelection(deleteObjects)  // !
    }
  }

  def deleteObjects(xs: Iterable[Any])(implicit tx: Tx): Unit
}

I found this question, however it does not deal with the lifting from methods to functions as far as I can see.

like image 403
0__ Avatar asked May 07 '13 08:05

0__


1 Answers

Implicits only work for methods. But you have to pass a function to withSelection. You can get around by wrapping the method in a function:

withSelection(a => b => deleteObjects(a)(b))

Its impossible to pass deleteObjects directly because foo _ does not work for a foo with an implicit parameter list defined.

like image 55
Martin Ring Avatar answered Dec 13 '22 09:12

Martin Ring