小编ego*_*doe的帖子

如何编写零参数函数?

我想要实现的是将 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)

scala function-composition

7
推荐指数
1
解决办法
263
查看次数

标签 统计

function-composition ×1

scala ×1