我试图创建一个像:
这样的函数指针def foo(a: String): Unit = { println(s"a = $a") }
val parameter = "some parameter"
// Here I'd want something like foo(parameter) _, but that doesn't work.
val partialFooWithParameter: () => Unit = ???
有什么东西我可以替换???
那会起作用,或者这在Scala中是不可能的?
更新:似乎答案是否。
最佳解决方案是函数文字:
def partialFooWithParameter: () => Unit = () => foo(parameter)
虽然它仍然不是部分应用的功能:
def bar(a: String, b: String): String = a + b
// Partially-applied.
val partial: (String => String) = bar("a", _)
// Function literal.
val literal: (String => String) = b => bar("a", b)
答案 0 :(得分:0)
我认为这就是你要找的东西。你的一些类型不正确。
def foo(a: String): Unit = { println(s"a = $a") }
val parameter = "some parameter"
// Here I'd want something like foo(parameter) _, but that doesn't work.
def partialFooWithParameter: () => Unit = () => foo(parameter)