In javascript, we can do something like:
value = f1() || f2() || f3();
this will call f1, and assign it to value if the result is not null.
only if the result is null, it will call f2, and assign it to value if that is not null.
…
A way to achieve this in scala is given here: How to make this first-not-null-result function more elegant/concise?
create a getFirstNNWithOption function that calls each function until not null:
value = getFirstNNWithOption(List(f1 _, f2 _, f3 _))
However, this is not as nice as the javascript || operator, which is more flexible. eg:
value = f1() || f2(3) || f3(44, 'ddd') || value4;
is there a way to achieve this in scala?
I am using the recommended
Optioninstead ofnull.For example: