Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit in scala - Not able to understand

Tags:

scala

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

like image 441
LPD Avatar asked Apr 24 '13 17:04

LPD


1 Answers

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.

like image 81
om-nom-nom Avatar answered Sep 20 '22 23:09

om-nom-nom