Monday, February 18, 2019

Inline Types and Functions in Swift

John Sundell:

Inlining can be a great tool for situations when we need an additional type or function, but we don’t want to expose it outside of the scope that it’s being used in. By placing that kind of simpler, more narrowly scoped, types and functions right next to the code that uses them — we can also make our code a bit easier to navigate and work with, by reducing context switching.

The fact that we can choose to define types and functions not only at the top level of a program, but also inline within other types or functions, is another example of just how flexible Swift is in terms of structure and syntax. However, just like with other features of the same nature, it’s important not to take things too far.

I really like using nested declarations and access control modifiers to make code more readable. A downside is that this can make stack traces less readable because the mangled names end up longer than if you had picked a globally unique name yourself.

Comments RSS · Twitter

Leave a Comment