3

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.

0

2 Answers 2

8

(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.

Sign up to request clarification or add additional context in comments.

2 Comments

"parameters of types T1 through Tn", so functions like f(T1):Unit, f(T2):Unit will be valid type for that list? What about something like f(T1,T2)?
@aitchnyu No. If you have a list of type List[(T1, T2) => Unit] then that list stores functions that take two parameters (the first one having type T1 and the second T2) and return type Unit. So you could store a function defined as f(x:T1, y:T2):Unit in it, but not one defined as f(x:T1) or f(y:T2). Likewise a list of type List[() => Unit] only stores functions that take exactly no parameters.
4

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.