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.
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.
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.I'm assuming they are similar to Option in Scala and Optional in Java? If so, they are amazing.
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 thatfoo
will never be nil, because if I write code that even placesfoo
at risk of being nil, my program won't compile! Pretty awesome stuff.I also like the guard statement in swift.