Using guard case
Another keyword which supports patterns is the newly introduced
keyword. You know how it allows you to bind
Optionals into the local
scope much like
if let only without nesting things:
print(a) } example( )
guard let case allows you to do something similar with the power that
pattern matching introduces. Let's have a look at our soldiers again.
We want to calculate the required HP until our player has full health
again. Soldiers can't regain HP, so we should always return 0 for a
let MAX_HP = 100 return MAX_HP - hp } print( , healthHP(Entity.Entry(type: .soldier, x: 10, y: 10, hp: 79))) print( , healthHP(Entity.Entry(type: .player, x: 10, y: 10, hp: 57))) // Prints:
This is a beautiful example of the culmination of the various mechanisms we've discussed so far.
- It is very clear, there is no nesting involved
- Logic and initialization of state are handled at the top of the
funcwhich improves readability
- Very terse.
This can also be very successfully combined with
wrap complex logical constructs into an easy to read format. Of course,
that won't make the logic any easier to understand, but at least it
will be provided in a much saner package. Especially if you use