<head></head><body>In a strongly typed language, such as Swift is, type inference is greatly appreciated as it declutter the instance type definition.<div><div><br></div><div>For example, in the following statement, the compiler easily deduct the type of `aString` by the return type of `String.init()`</div><div><br></div><div>```</div><div>let aString = String()</div><div>```</div><div><br></div><div>Optional are generic enum which can contain a value of a generic type or nil (https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift)</div><div><br></div><div><span style="line-height: 19.6px;">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><span style="line-height: 19.6px;">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;">For example, you may want specify an optional String with a default value.</div><div><br></div><div>```</div><div>var a:String? = String()</div><div>// or</div><div>var b = String() as String?</div><div>// or</div><div>var c:Optional<String> = String()<br></div><div>```</div><div><br></div><div>But the compiler can already infer the variable type from this assignment, it just miss the "optionality" of the variable.</div><div>It would be nice to be able to express this.</div><div><br></div><div>Then for, I propose the following syntax evolution:</div><div><br></div><div>```</div><div>var a:? = String()</div><div>// and/or (not recommended because more prone to typo and unclear...)</div><div>var a = String() as?</div><div>```</div><div><br></div><div>This would allow for more synthetic optional declaration.</div><div><br></div><div>I've draft a proposal on my `swift-evolution` fork:</div><div><span class="Apple-tab-span" style="white-space:pre">        </span><span style="line-height: 1.4;">https://github.com/huguesbr/swift-evolution/blob/optionality-type-with-type-inference/proposals/0057-optionality-type-with-type-inference.md</span></div><div><span style="line-height: 1.4;"><br></span></div><div><span style="line-height: 1.4;">Let me know what you think.</span></div><div><br></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;"><br></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);">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;"><br></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);">--</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);">hugues@xdev.fr</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);">http://www.xdev.fr</p><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;">
</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);">http://www.linkedin.com/in/huguesbr</p></div></div></body>