Merits of the "unless" keyword

I suspect unless let … could get rid of quite a few cases of either redundant checks (guard let … when you actually know the variable is already non-nil) or force-unwraps (which are dangerous because the code might change one day to break the expectation, and you might only find out in production rather than at compile time).

This line of function exploration makes me think unless should not permit else clauses; it should be modelled after guard, instead. As noted earlier, if…else already serves the same purpose - merely with the block orders reversed - and is easier to comprehend.

Note though that there are other possible solutions for the optional binding problem, e.g.:

token ??= do { … }

(building on the do expressions idea that's been pitched before)

Though that example has quite a bit more 'magic' in it (the reader and the compiler must understand that if the do expression returns a non-optional value, token is non-optional following that line, etc).

1 Like