Jeremy W. Sherman

stay a while, and listen

No Single Swift Style

“Swift is too young for us to say what good Swift style is, or to have developed a sense for idiomatic Swift.”

Talks, blogs, and books repeat this sentiment or variations.

This statement assumes that there will eventually develop a single, canonical Swift style.

This is a mistake.

I expect no single style will develop; instead, several different styles will flourish. Perhaps most developers will be conversant with multiple styles. More likely, these styles will develop into several mutually unintelligible dialects.

What might these styles be?

  • Obj-C with different syntax: Existing frameworks exert a strong pull in this direction. Frankly, it feels awkward to me in Swift, but I expect a lot of code to be written this way.
  • OOP: A Swift-flavored OOP dialect will likely emerge from the ashes of Obj-C. Many are comfortable with OOP, and they’ll stick with it, using Swift as their new, less brackety vehicle.
  • Generic Programming: Swift’s small standard library exemplifies this, though it compromises in places by having both a generic function and an instance method, rather than picking a single one. I expect the latter is often there to assist discoverability.
  • Functional Programming: There has been a lot of interest in the dataflow aspects of functional programming across computing lately. Swift makes no exception.

    In addition, Swift’s type system makes it possible to borrow at least some of the approaches pioneered by the ML family of languages, though its lack of support for higher-kinded types and incomplete support for variably-sized enum types can make this awkward to express. With a solid Prelude library to provide basic tools missing from Swift’s standard library, FP in Swift will be a very real possibility. Higher-level FP programming will likely be no less foreign to the other dialects in a couple years than it is today; reliance on FP-specific libraries will not help.

Swift’s styles are being built today by the snippets and libraries that will constitute their core vocabulary.

Some idioms will be common to several styles; some will be unique to a style; some of those will be inapplicable, even inexpressible, in other styles.

Swift isn’t too young to have a style: It’s just too big to be confined to having a single style.