Akka child returns Option
As you can see from Option scaladoc:
fold [B] (ifEmpty: β B) (f: (A) β B): B Returns the result by applying f to this scala.Option value if scala.Option is empty. Otherwise, the ifEmpty expression is evaluated.
Or make it more clear:
This (fold) is equivalent to scala. Use map f getOrElse ifEmpty.
So, the first parameter fold is lazy (call sign) and is evaluated only if Option empty. The second parameter (function) is called only if Option not empty.
Experiment:
scala> Some(0).fold({println("1");1}){_ => println("2"); 2} 2 res0: Int = 2 scala> None.fold({println("1");1}){_ => println("2"); 2} 1 res1: Int = 1
Here are some indications about:
https://kwangyulseo.com/2014/05/21/scala-option-fold-vs-option-mapgetorelse/
And some critics of this approach:
http://www.nurkiewicz.com/2014/06/optionfold-considered-unreadable.html
But in Option.fold (), the contract is different: the folding function accepts only one parameter, not two. If you read my previous article about bends, you know that the reduction function always takes two parameters: the current element and the accumulated value (the initial value during the first iteration). But Option.fold () accepts only one parameter: the current value of the parameter! This breaks consistency, especially when the implementation of Option.foldLeft () and Option.foldRight () are correct (but this does not mean that they are more readable).
source share