In the following code example, I do not understand why the function fun can be passed as an argument to the method addAction
. The method fun
is of
There is a difference between methods and functions. In your case actions
is a list of functions. When the compiler knows that a function is required (like in the case of addAction
) it can automatically convert a method fun
into a function. Now ::
is also a method, therefore the compiler also knows that it takes functions as parameters. But the problem is the syntactic sugar of the right-associative operator ::
. If you were to call it like a method: actions.::(fun)
it will compile (although I can't test it at the moment). When writing fun :: actions
the compiler thinks that fun
is an expression and therefore evaluates it and since it "returns" a Unit
you get your compiler error.
EDIT
Since I now have the possibility to test my hypothesis (which was wrong) here are your options:
// Usual syntax
actions.::[() => Unit](fun)
actions.::(fun: () => Unit)
actions.::(fun _)
// Operator syntax
(fun: () => Unit) :: actions
(fun _) :: actions