Am bogged down by this syntax:
def foreach(f: Tweet => Unit): Unit = {
f(elem)
left.foreach(f)
right.foreach(f)
}
where Tweet
is a class with three variables. What does it mean for a function to return Unit
? I tried different things but am not able to call the function itself in this case.
Please help. Thanks
Unit in scala is identical to void in Java, moreover, Java sees Scala's Unit as void, and vise versa. So in java it could be written like:
void foreach(MethodThatReturnsVoid f) {
f.apply(elem)
left.foreach(f)
right.foreach(f)
}
It is a little bit pseudocode (since java doesn't support first class functions yet) but I hope you've got the idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With