After a year translating C++ code, I've a long list of beloved features: the lack of header files, named parameters, ARC, efficient value-semantic collections, Unicode String, optionals, closures, enumerations with associated values, Swift Concurrency, and all the sugar that enables concise yet clear code, which let me rewrite 152,000 lines of C++ into 29,000 lines of Swift.
But the one thing I'm far too enamored of is functional programming using one-line single-expression read-only computed properties.
Value semantics, type inference, Optional, library-defined fundamental types, argument labels, generics model (including protocol + extension), enum, real Unicode String
Most promising new feature: Swift Concurrency (which is still too young and pretty rough around the edges to make it to the main list)
swift projects are remarkably self-documenting if written properly. iβm always pleasantly surprised how easy it is to pick up uncommented code i wrote months or even years ago.
Function arguments having visible outside names is great, and the fact that they read almost like English sentences, for example type(of: 123)
Also the fact that Swift uses full words that have meaning, instead of weird abbreviations like other languages! FLT_MIN in C is confusing. You can guess that it's the minimum value of float, but you would be wrong.
the distinction between sort and sorted methods
no weird distinction where some of the types are written fully lowercase, and some are CamelCase
It's not anything complicated, you don't need any compiler features to write good names, but for some reason a lot of languages don't do that.
I think the fact that Appleβs technical writing staff was involved at an early stage must have made a big difference. Most languages are designed in programmer/implementer echo chambers, with perhaps Smalltalk as a notable counter-example.
I like a lot about Swift, really a lot, but probably my top 2 are:
progressive disclosure, more of a philosophy than a feature, but a philosophy behind many Swift features, allows to start simple and gradually include more complex mechanics in your code, and makes learning and teaching Swift a breeze;
easy-to-use, ergonomic value types, and the focus on value semantics are, to me, the most important Swift features by far, they completely changed the way I think about software engineering in general, and they make Swift stand out among languages that seem superficially similar but are in fact profoundly different, like for example Kotlin.
The fact that it runs everywhere , is compiled, and is a cleaner language than either Rust or C++. For high productivity and manageability at scale, it competes with Java while being easier to deploy.
Upon true reflection on the question, the real star feature of Swift is, unequivocally, this community. Our collective care for the language, help for each other, and stewardship for its future makes us absolutely the best thing about Swift.
Yeah, the signal/noise ratio reminds me of the earlier days of usenet news (comp.sys.next.programmer and related feeds specifically). The active participation of the language core team and other experienced users from both Apple and others (ranging from Indies, academia all the way to other industry players) is a key asset without a doubt. It's definitely one important factor for us.
First class support for optionals and error handling are the two things that stand out the most for me. Especially the latter makes you think βthis is so obvious, how come no other language came up with this syntax before Swift?β. The fact that at the same time it managed to bridge legacy (i.e. Obj-C error handling) as well in such an elegant way still amazes me to this day.
Value semantics, specifically the fact that the mutability of an instance of a struct depends only on the variable it's assigned to (whether it's let or var).
This is what enables all swift data structures to avoid having to offer a mutable and a non mutable variant, which is amazing, and something unique to Swift among the mainstream languages if I'm not wrong.
struct Foo {
const int x = 0;
int y = 0;
};
struct Bar {
const int x = 0;
int y = 0;
const Foo foo1 = Foo();
Foo foo2 = Foo();
};
void test() {
const Foo foo1 = Foo();
Foo foo2 = Foo();
const Bar bar1 = Bar();
Bar bar2 = Bar();
foo1.x = 1; // β Cannot assign to non-static data member 'x' with const-qualified type 'const int'
foo1.y = 1; // β Cannot assign to variable 'foo1' with const-qualified type 'const Foo'
foo2.x = 1; // β Cannot assign to non-static data member 'x' with const-qualified type 'const int'
foo2.y = 1;
bar1.x = 1; // β Cannot assign to non-static data member 'x' with const-qualified type 'const int'
bar1.y = 1; // β Cannot assign to variable 'bar1' with const-qualified type 'const Bar'
bar1.foo1.x = 1; // β Cannot assign to non-static data member 'x' with const-qualified type 'const int'
bar1.foo1.y = 1; // β Cannot assign to non-static data member 'foo1' with const-qualified type 'const Foo'
bar1.foo2.x = 1; // β Cannot assign to non-static data member 'x' with const-qualified type 'const int'
bar1.foo2.y = 1; // β Cannot assign to variable 'bar1' with const-qualified type 'const Bar'
bar2.x = 1; // β Cannot assign to non-static data member 'x' with const-qualified type 'const int'
bar2.y = 1;
bar2.foo1.x = 1; // β Cannot assign to non-static data member 'x' with const-qualified type 'const int'
bar2.foo1.y = 1; // β Cannot assign to non-static data member 'foo1' with const-qualified type 'const Foo'
bar2.foo2.x = 1; // β Cannot assign to non-static data member 'x' with const-qualified type 'const int'
bar2.foo2.y = 1;
}
Absolutely enums. They've massively changed how I build a lot of code and there's almost nothing bad about them.
Flipping things slightly, I could say that protocols also stand out a huge amount, but mostly because they form the bulk of the "hate" part of my love-hate relationship with Swift
enum is enum, even if its rawValue is Int. Or if it's not RawRepresentable it's only enum. I missed this very much when I'm in the other languages.
More specifically its strictness and safeness.
I'd say enums, but IMHO they should be more ergonomic, e.g.:
enum E {
...
case a(x: Int, Int)
}
var e = E.a(x: 1, 2)
let v = e.a // this is of type `(x: Int, Int)!`
e.a.x = 100 // change associated value field by name
e.a.1 = 200 // change associated value field by index
if let v = e.a { // optional binding
v.0 ...
v.y ...
}
let isA = e.a != nil
Just want to give a big thank you to everyone for the interesting responses so far. Thereβs no time limit here, so feel free to share if you havenβt already!