<div dir="ltr">Hi Hughes,<div><br></div><div>What&#39;s wrong with:<br>let s = Optional(String(&quot;abc&quot;))<br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 24, 2016 at 12:22 PM, Hugues Bernet-Rollande via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>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 (<a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift" target="_blank">https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift</a>)</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&lt;String&gt; = 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 &quot;optionality&quot; 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&#39;ve draft a proposal on my `swift-evolution` fork:</div><div><span style="white-space:pre-wrap">        </span><span style="line-height:1.4"><a href="https://github.com/huguesbr/swift-evolution/blob/optionality-type-with-type-inference/proposals/0057-optionality-type-with-type-inference.md" target="_blank">https://github.com/huguesbr/swift-evolution/blob/optionality-type-with-type-inference/proposals/0057-optionality-type-with-type-inference.md</a></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><p style="margin-bottom:0px;font-stretch:normal;line-height:normal;font-family:Avenir;min-height:19px"><br></p><p style="margin-bottom:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:&#39;Sofia Pro Light&#39;">Hugues BERNET-ROLLANDE</p><p style="margin-bottom:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:&#39;Sofia Pro Light&#39;;min-height:16px"><br></p><p style="margin-bottom:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:&#39;Sofia Pro Light&#39;">--</p><p style="margin-bottom:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:&#39;Sofia Pro Light&#39;"><a href="mailto:hugues@xdev.fr" target="_blank">hugues@xdev.fr</a></p><p style="margin-bottom:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:&#39;Sofia Pro Light&#39;"><a href="http://www.xdev.fr" target="_blank">http://www.xdev.fr</a></p><p style="margin-bottom:0px;font-stretch:normal;line-height:normal;font-family:Avenir;min-height:19px">





</p><p style="margin-bottom:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:&#39;Sofia Pro Light&#39;"><a href="http://www.linkedin.com/in/huguesbr" target="_blank">http://www.linkedin.com/in/huguesbr</a></p></div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>