What does `() => Unit` mean?

As in the question.

This was found in List[() => Unit]

which was used to store callback functions.

I understand List[type]

and what Unit

is the return type of a function that returns nothing.

+3


source to share


2 answers


(T1,...,Tn) => T

is the type of functions that take type parameters T1

through Tn

and return a type T

. Thus, () => Unit

is the type of functions that take no parameters and return type Unit. Hence, List[() => Unit]

is a type of lists containing such functions.



+8


source


First, you have a list of features. Each function takes no parameters (that's what the open-close parentheses mean ()

) and returns Unit

, which is a value with no result, similar to void.



In a pure functional world, a type function is ()=>Unit

useless because it accepts nothing and returns nothing. However, Scala is not a purely functional language; it has side effects. To be useful, the functions on the list will undoubtedly have side effects. Since they are callback functions, they also have an idea of ​​when they fire.

+4


source







All Articles