<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="">Could you give us one or two real-world examples where you’d want to do this?</div><div class=""><br class=""></div><div class="">I really can’t think of a situation where I would want to assign a non-optional to an optional variable. Even if I transform it later such that it might produce nil, it’s desirable to assign it to two different constants:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let a = String()</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let b = a.thisMightMakeMeNil()</div><br class=""><div class="">
<div class="">— Radek</div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On 24 Mar 2016, at 11:22, Hugues Bernet-Rollande via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">In a strongly typed language, such as Swift is, type inference is greatly appreciated as it declutter the instance type definition.<div class=""><div class=""><br class=""></div><div class="">For example, in the following statement, the compiler easily deduct the type of `aString` by the return type of `String.init()`</div><div class=""><br class=""></div><div class="">```</div><div class="">let aString = String()</div><div class="">```</div><div class=""><br class=""></div><div class="">Optional are generic enum which can contain a value of a generic type or nil (<a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift" class="">https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift</a>)</div><div class=""><br class=""></div><div class=""><span style="line-height: 19.6px;" class="">Sometime you may want specify that the instance is of type optional in which even if you are a the same time assigning a value to this instance.</span></div><div class=""><span style="line-height: 19.6px;" class="">In which case you loose the type inference mechanism and you have to define both (the optionality as well as the type). </span></div><div style="line-height: 19.6px;" class="">For example, you may want specify an optional String with a default value.</div><div class=""><br class=""></div><div class="">```</div><div class="">var a:String? = String()</div><div class="">// or</div><div class="">var b = String() as String?</div><div class="">// or</div><div class="">var c:Optional<String> = String()<br class=""></div><div class="">```</div><div class=""><br class=""></div><div class="">But the compiler can already infer the variable type from this assignment, it just miss the "optionality" of the variable.</div><div class="">It would be nice to be able to express this.</div><div class=""><br class=""></div><div class="">Then for, I propose the following syntax evolution:</div><div class=""><br class=""></div><div class="">```</div><div class="">var a:? = String()</div><div class="">// and/or (not recommended because more prone to typo and unclear...)</div><div class="">var a = String() as?</div><div class="">```</div><div class=""><br class=""></div><div class="">This would allow for more synthetic optional declaration.</div><div class=""><br class=""></div><div class="">I've draft a proposal on my `swift-evolution` fork:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="line-height: 1.4;" class=""><a href="https://github.com/huguesbr/swift-evolution/blob/optionality-type-with-type-inference/proposals/0057-optionality-type-with-type-inference.md" class="">https://github.com/huguesbr/swift-evolution/blob/optionality-type-with-type-inference/proposals/0057-optionality-type-with-type-inference.md</a></span></div><div class=""><span style="line-height: 1.4;" class=""><br class=""></span></div><div class=""><span style="line-height: 1.4;" class="">Let me know what you think.</span></div><div class=""><br class=""></div><div class="nylas-n1-signature"><p style="margin-bottom: 0px; font-stretch: normal; line-height: normal; font-family: Avenir; -webkit-text-stroke-color: rgb(0, 0, 0); min-height: 19px;" class=""><br class=""></p><p style="margin-bottom: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Sofia Pro Light'; -webkit-text-stroke-color: rgb(0, 0, 0);" class="">Hugues BERNET-ROLLANDE</p><p style="margin-bottom: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Sofia Pro Light'; -webkit-text-stroke-color: rgb(0, 0, 0); min-height: 16px;" class=""><br class=""></p><p style="margin-bottom: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Sofia Pro Light'; -webkit-text-stroke-color: rgb(0, 0, 0);" class="">--</p><p style="margin-bottom: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Sofia Pro Light'; -webkit-text-stroke-color: rgb(0, 0, 0);" class=""><a href="mailto:hugues@xdev.fr" class="">hugues@xdev.fr</a></p><p style="margin-bottom: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Sofia Pro Light'; -webkit-text-stroke-color: rgb(0, 0, 0);" class=""><a href="http://www.xdev.fr" class="">http://www.xdev.fr</a></p><div style="margin-bottom: 0px; line-height: normal; font-family: Avenir; -webkit-text-stroke-color: rgb(0, 0, 0); min-height: 19px;" class="">
<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Sofia Pro Light'; -webkit-text-stroke-color: rgb(0, 0, 0);" class=""><a href="http://www.linkedin.com/in/huguesbr" class="">http://www.linkedin.com/in/huguesbr</a></p></div></div></div>_______________________________________________<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>