scala – 部分应用具有隐式参数的函数
发布时间:2020-12-16 09:20:42 所属栏目:安全 来源:网络整理
导读:我可以把一个隐式参数转换成一个函数吗? trait Txdef foo(bar: Any)(implicit tx: Tx) {}foo _ // error: could not find implicit value for parameter tx: Tx 我试图实现以下,最好是如果我可以以某种方式使它与简单的调用withSelection(deleteObjects)工
我可以把一个隐式参数转换成一个函数吗?
trait Tx def foo(bar: Any)(implicit tx: Tx) {} foo _ // error: could not find implicit value for parameter tx: Tx 我试图实现以下,最好是如果我可以以某种方式使它与简单的调用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 } 我发现了this question,但是,尽管我看不到从方法到功能的解决方案. 解决方法
意义只适用于方法.但是你必须将一个函数传递给withSelection.你可以通过在一个函数中包装方法来解决:
withSelection(a => b => deleteObjects(a)(b)) 它不可能直接传递deleteObjects,因为foo _不适用于定义了隐式参数列表的foo. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |