在下面的代码示例中,我不明白为什么函数fun可以作为参数传递给方法addAction.该方法fun属于类型Unit,而该方法addAction需要类型的函数() => Unit.
如果fun是类型() => Unit,那么当我尝试添加到操作列表时,为什么编译器会抱怨fun类型?Unitfunactions = fun :: actions
package myscala
object MyScala {
def fun() { println("fun1 executed.") }
def addAction(a: () => Unit) {
actions = a :: actions
}
var actions: List[() => Unit] = List()
def main(args: Array[String]) {
// the following line would produce a compiler error (found: Unit, required: () => Unit), it's OK …Run Code Online (Sandbox Code Playgroud)