Swift: Unwrapped optionals defeat the purpose of their existance

Originator:owensd
Number:rdar://17312572 Date Originated:13-Jun-2014 09:11 PM
Status:Opened Resolved:
Product:Developer Tools Product Version:Xcode 6 Beta
Classification:Enhancement Reproducible:Always
 
By allowing optionals to be implicitly unwrapped, you have re-created the nil problem that you were trying to avoid in the first place.

While more verbose, this code will always work. I thought that was one of the goals of Swift: to create safe code by default.

func badIncrement(value: Int!) -> Int { return value + 1 }
func safeIncrement(value: Int?) -> Int {
    assert(value != nil)
    if let safeValue = value { return safeValue + 1 }
    return 0
}

badIncrement(nil) // CRASH!
safeIncrement(nil) // caught at debug time, doesn't cause crash on ship

Please consider removal of implicitly unwrapped optionals, they are never safe.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!