我想要实现的是将 2 个函数(其中一个是没有 arg 函数)合二为一。
这是一个示例,可以让您了解我在做什么:
object Test extends App {
val zeroArgFunc = () => 10
val intArgFunc = (i: Int) => s"hello $i"
val stringArgFunc = (s: String) => println(s)
// This line works perfectly fine.
val intAndThenString: Int => Unit = stringArgFunc compose intArgFunc
// But this line fails with 'type mismatch' compilation error.
val zeroAndThenInt: () => String = intArgFunc compose zeroArgFunc
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
[error] found : () => Int
[error] required: ? => Int
[error] …Run Code Online (Sandbox Code Playgroud)