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?
Conversation
Notices
-
Embed this notice
Nick Lockwood (nicklockwood@mastodon.social)'s status on Thursday, 09-May-2024 01:12:29 JST Nick Lockwood -
Embed this notice
Paul Cantrell (inthehands@hachyderm.io)'s status on Thursday, 09-May-2024 01:12:28 JST 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
-
Embed this notice
Nick Lockwood (nicklockwood@mastodon.social)'s status on Thursday, 09-May-2024 01:22:11 JST Nick Lockwood @inthehands these are great, thank you!
-
Embed this notice
Paul Cantrell (inthehands@hachyderm.io)'s status on Thursday, 09-May-2024 01:22:11 JST Paul Cantrell @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.
-
Embed this notice
Paul Cantrell (inthehands@hachyderm.io)'s status on Thursday, 09-May-2024 02:37:06 JST Paul Cantrell @_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! -
Embed this notice
Andreas Hartl (_nd_@fnordon.de)'s status on Thursday, 09-May-2024 02:37:11 JST Andreas Hartl @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.
-
Embed this notice