<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">This is definitely something I’m hoping to see as well, alongside more intelligent handling of the is keyword, as currently Swift doesn’t handle the following either:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if foo is SomeType { /* foo could be safely used as SomeType here, but currently is not */ }</font></div><div class=""><br class=""></div><div class="">Hopefully someone more familiar can weigh in, as it seems like something I expect to be on the way but perhaps has been delayed in case any further changes to the type system were required?</div><br class=""><div><blockquote type="cite" class=""><div class="">On 29 Apr 2016, at 15:37, Tod Cunningham via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">I'm new to the swift evolution community, but I wanted to toss an idea out there to get some feedback on it. So here it goes...<br class=""><br class="">Currently, if you assign a non-nil value to an optional and then want to access that optional later, in the same context, you need to manually unwrap the value. &nbsp;This is usually done either by using "!" or by using something like "if let" or guard.<br class=""><br class="">What would it be like if the compiler could auto unwrap, in cases where in knows the optional will have some value? &nbsp;This would make the code "clean" and still be safe.<br class=""><br class="">This concept of Auto Unwrapping of Optionals is similar to Implicitly Unwrapped Optionals, but is only applied when the compiler knows it is safe to do so.<br class=""><br class="">Take the following example:<br class=""><br class="">class Test {<br class=""> &nbsp;&nbsp;&nbsp;var today: NSDate? = nil<br class=""> &nbsp;&nbsp;&nbsp;func test() {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;today = today ?? NSDate()<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Today is \(today)") &nbsp;&nbsp;// Would be printed as an optional<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let timeInterval: NSTimeInterval = today!.timeIntervalSinceNow &nbsp;// Requires ! or (if let) to unwrap<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ... do stuff with timeInterval ...<br class=""> &nbsp;&nbsp;&nbsp;}<br class="">}<br class=""><br class="">With the above example, the compiler could known that today has a value after it's set in the test method. &nbsp;So why couldn't the compiler auto unwrap it when accessed? &nbsp;This would mean manual unwrapping would be unnecessary:<br class=""><br class="">class Test {<br class=""> &nbsp;&nbsp;&nbsp;var today: NSDate? = nil<br class=""> &nbsp;&nbsp;&nbsp;func test() {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;today = today ?? NSDate()<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Today is \(today)") &nbsp;&nbsp;// Would be printed as a value (not an optional)<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let timeInterval: NSTimeInterval = today.timeIntervalSinceNow &nbsp;// No ! required (auto unwrapped)<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ... do stuff with timeInterval ...<br class=""> &nbsp;&nbsp;&nbsp;}<br class="">}<br class=""><br class="">If the value later gets set to an optional value, then it will no longer be auto unwrapable :<br class=""><br class="">class Test {<br class=""> &nbsp;&nbsp;&nbsp;var today: NSDate? = nil<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func optionalDay() -&gt; NSDate? {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NSDate()<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func test() {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;today = today ?? NSDate()<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Today is \(today)") &nbsp;&nbsp;// Would be printed as a value (not an optional)<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let timeInterval: NSTimeInterval = today.timeIntervalSinceNow &nbsp;&nbsp;&nbsp;// No ! required (auto unwrapped)<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let timeInterval2: NSTimeInterval = today!.timeIntervalSinceNow &nbsp;// Explicit unwrapping would still be allowed<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// If today is assigned an optional value, we can no longer auto unwrap it<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;today = optionalDay()<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Today is \(today)") &nbsp;&nbsp;// Would be printed as an optional<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let timeInterval3: NSTimeInterval = today!.timeIntervalSinceNow &nbsp;// manual unwrapping would be required<br class=""> &nbsp;&nbsp;&nbsp;}<br class="">}<br class=""><br class="">Note in the above example, explicit unwrapping would still be allow. &nbsp;The variable is still an optional. &nbsp;This allows for existing code to remain unchanged.<br class=""><br class="">This change would encourage less use of forced unwrapping "!", generally require the developer to write less code, and would maintain code safety. &nbsp;On the down side, it is performing some compiler “magic”. &nbsp;It would be yet another thing to explain when trying to introduce people to swift and especially optionals.<br class=""><br class="">What do you all think, would something like this be worth pursuing, what other pluses or minus would this introduce, has something like this already been discussed?<br class=""><br class="">Thanks,<br class="">Tod Cunningham<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>