hachyderm.io is one of the many independent Mastodon servers you can use to participate in the fediverse.
Hachyderm is a safe space, LGBTQIA+ and BLM, primarily comprised of tech industry professionals world wide. Note that many non-user account types have restrictions - please see our About page.

Administered by:

Server stats:

9.5K
active users

In a fit of madness I've agreed to give an internal talk about advanced Swift at my company. Has anybody got any favorite Swift tricks they'd like to share?

Paul Cantrell

@nicklockwood
Don’t know if these qualify as “advanced” so much as “oft overlooked,” but:

- reduce(into:) is useful

- underscores can make large numbers readable: 1_000_000

- ContinuousClock is useful

- If you want an “open enum” (i.e. clients of lib can add cases), don’t use enum; use e.g. `protocol Color` + possibly empty `struct Mauve: Color` for cases, then match with `value is Mauve`, or `case let value as Mauve` if you need to narrow the type of `value` when matched

@nicklockwood You bet! Lmk if the open enum one is incomprensible; it was hard to fit in char limit. Can provide sample code if useful.

@inthehands @nicklockwood on the other hand the difference beween frozen enums and non-frozen ones + @‌unknown default: is interesting from a library evolution perspective – compare and contrast ComparisonResult where three cases are all there ever will be and LABiometryType where Vision Pro added a new opticID case.

@_nd_ @nicklockwood
AIUI, frozen vs non-frozen is about library evolution, not extensibility: it allows the library itself to add future cases without breaking existing code, but does not (I think?) allow library •clients• to add new cases. Both are useful. Def worth going over the difference if Nick decides to tackle it!