DEV Community

Discussion on: What language features/concepts do insiders of the language love and outsiders hate?

Collapse
 
twof profile image
Alex Reilly

Optionals in Swift.

When I first started learning the language, I didn't understand at all why I'd want to decide if a value could be nil or not. It just felt like extra unneeded complexity, but after writing Swift for over a year, it's soooo much easier to reason about code when you can be sure you don't have to think about an entire set of fail cases.

It's also fantastic when you can take an optional, unwrap it at one level, and never have to worry about it being nil again no matter where you pass the value.

Collapse
 
evanoman profile image
Evan Oman

I'm assuming they are similar to Option in Scala and Optional in Java? If so, they are amazing.

Collapse
 
twof profile image
Alex Reilly

Yup! Exactly that, but they're supported at the language level and every variable is, by default, not optional.

So if I write var foo: Int = 0, I can be sure that foo will never be nil, because if I write code that even places foo at risk of being nil, my program won't compile! Pretty awesome stuff.

Collapse
 
francisco profile image
Francisco M. Delgado

I also like the guard statement in swift.