You may have noticed that our two functions above used different blocks; the first being when
and the second if
. You will often see one or the other used as a conditional, but it's not always immediately apparent why. In general, the reasons to use when
are:
- There is no else-part associated with the result of a conditional
- You require an implicit
do
in order to perform side-effects
The reasons for the use of if
would therefor be the inverse of those above.