by masklinn · 9y ago · 0 comments
You'd think so, but I've had colleagues who managed to fuck that up and use map or list comprehension solely for side-effects.
def update(): Try[Unit] = {
parser.parse(...).map(result => updateState(result))
}
And I thought it was abusing the map() call for side effects. However, it is still shorter than writing it out as follows: def update(): Try[Unit] = {
parser.parse(...) match {
case Success(result) =>
updateState(result)
Success(())
case f@Failure(_) =>
f
}
}
So I didn't have a strong opinion either way since semantically both do the same (and in the case of Scala, the first one is potentially more performant since it relies on the JVM doing virtual dispatch as opposed to calling unapply() and matching, not to mention potentially less garbage being generated).