Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `() => Unit` mean?

Like the question says.

This was found in List[() => Unit] which was used to store callback functions.

I understand List[type] and that Unit is a return type of a function that doesnt return anything.

like image 963
Jesvin Jose Avatar asked Dec 05 '25 22:12

Jesvin Jose


2 Answers

(T1,...,Tn) => T is the type of functions that take parameters of types T1 through Tn and return type T. So () => Unit is the type of functions that take no parameters and have return type Unit. Consequently List[() => Unit] is the type of lists containing such functions.

like image 125
sepp2k Avatar answered Dec 07 '25 10:12

sepp2k


First off, you have a list of functions. Each function takes no parameters (this is what the open-close parentheses () mean) and returns Unit, which is a value for no result, similar to void.

In a pure functional world, a function of type ()=>Unit is useless because it takes nothing and returns nothing. However, Scala is not a purely functional language; it has side effects. To be useful, the functions in the list will surely have side-effects. Since they are callback functions, they also have an idea about when they fire.

like image 44
schmmd Avatar answered Dec 07 '25 11:12

schmmd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!