Hacker News new | ask | show | jobs
by ChubbyGlasses 1453 days ago
I think there are different ways to look at boilerplate, some of which languages like C++ and Rust are great at handling through their standard libraries, but there is also code boilerplate that needs language support handle well. defer and C-style for loops being the most well known constructs where the language provides syntax for something that otherwise would be difficult to express with just functions and lifetimes. Swift goes a step beyond by providing syntax for many other common patterns (aka boilerplate).

For example, a few areas where Swift reduces boilerplate and the syntax it provides to do so:

  - a complete null/optional story: first class optional types (T? = Option<T>), null chaining (obj?.prop), and null coalescing (T ?? "default")

  - error handling: fn() throws Err (= fn() -> Result<T,Err>), try, try?, try!, do/catch (out-of-band error handling), w/o stack unwinding, see nulls

  - and struct/enum lifecycle management: constructors, getter/setter props, defaults, named functions, named tuples, etc. (this list is loong)

Individually each feature is small and could possibly be argued as bloat or overhead since you can accomplish the same with just regular functions and Rust-like code and abstractions, but combined they make the language feel more expressive (and more fun IMO) than would otherwise be possible and often without sacrificing performance. They also serve to reduce cognitive overhead in the same way using for loops is nicer than desugaring to while loops. This is nothing to say of the syntax itself, and a bunch of other small features; Swift really is a case where the whole is greater than the sum of of its parts.