0__*_*0__ 16 scala implicit currying partial-application
我可以将一个带隐式参数的方法转换为函数吗?
trait Tx
def foo(bar: Any)(implicit tx: Tx) {}
foo _ // error: could not find implicit value for parameter tx: Tx
Run Code Online (Sandbox Code Playgroud)
我试图实现以下,最好是我可以以某种方式使它与普通调用一起工作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
}
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题,但就我所见,它并没有解决从方法到功能的问题.
仅涉及方法的工作.但是你必须传递一个函数withSelection.你可以通过在函数中包装方法来解决这个问题:
withSelection(a => b => deleteObjects(a)(b))
Run Code Online (Sandbox Code Playgroud)
它不可能deleteObjects直接传递,因为foo _对于foo定义了隐式参数列表的a不起作用.
| 归档时间: |
|
| 查看次数: |
2968 次 |
| 最近记录: |