What is the most idiomatic way to make a side effect if the value is Some (...) and make another side effect if the value is None. Here is what I would write now:
def doSideEffectA(value: Int) { // ... } def doSideEffectB() { // ... } def doSideEffect(valueOption: Option[Int]) { valueOption map { value => doSideEffectA(value) } getOrElse { doSideEffectB() } }
My problem is that if I don't need to do anything, if valueOption is None, here is what I would write:
def doSideEffectNothingIfNone(valueOption: Option[Int]) { valueOption foreach { value => doSideEffectA(value) } }
map / getOrElse is not usually used in the context of side effects, whereas foreach. I donβt really like the valueOption map {...} getOrElse {...} returning Unit, since I really donβt "get" anything from my [Int] parameter.
source share