3 ms·
Can you explain force unwrap? Actually I was wondering with Swift had a lot of these things I don't see much in other languages. Optionals for example. Is it b
by the_clarence 8y ago
Can you explain force unwrap?
Actually I was wondering with Swift had a lot of these things I don't see much in other languages. Optionals for example. Is it because it us focused on coding highly responsive UIs where different states might not yet be available?
- saagarjha 8y agoForce unwrapping is what it sounds like: you take an optional, and forcefully convert it to a nonoptional. Obviously there must be a valid value at this point in the program, or it will crash. Optionals are useful not only for UIs, but for handling any sort of state where it's possible for something to not have a valid value at any point in time. It just hoists such a state into the type system rather than checking this at runtime.
- jernfrost 8y agoIt is not much in other languages because those languages are old. A lot of the Swift features have been in more academic functional languages for a long time. However it has taken time for the mainstream languages to adopt these ideas. The industry moves much slower than academic niche languages. But most new languages has some variant of this stuff: kotlin, scala, rust. I program a lot in Julia which is a script language and it does not have allow you to use null freely either. May seem odd for a language that does not type check at compile time. But it actually makes sense.