<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=""><br class=""></div><div><blockquote type="cite" class=""><div class="">Le 11 janv. 2016 à 05:56, Douglas Gregor &lt;<a href="mailto:dgregor@apple.com" class="">dgregor@apple.com</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><blockquote type="cite" class="">On Jan 10, 2016, at 1:31 AM, David Scrève via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Hi Chris,<br class=""><br class=""><blockquote type="cite" class="">Le 10 janv. 2016 à 02:29, Chris Lattner &lt;<a href="mailto:clattner@apple.com" class="">clattner@apple.com</a>&gt; a écrit :<br class=""><br class=""><blockquote type="cite" class=""><br class=""></blockquote><br class="">This is certainly a glaring hole in the system, and one we need to discuss. &nbsp;Here are some problems with making optional requirements a first class thing in Swift:<br class=""><br class="">1. They overlap heavily (but are syntactically privileged) with optional properties, consider the difference between "optional func f() -&gt; Int" &nbsp;vs "var f : (() -&gt; Int)? &nbsp;{get}”.<br class=""></blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>You are right. I suggest that we restrict optional keyword in protocol to methods only to fix this issue and keep the actual meaning.<br class=""></blockquote><br class="">That wasn’t Chris’s point. Compare<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>protocol P {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;optional func f() -&gt; Int<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class="">with<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>protocol P {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;var f: (() -&gt; Int)? { get }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>extension P {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;var f: (() -&gt; Int)? { return nil }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class="">Both will have the same “call-side” syntax, e.g., accessing the “f” member of something that conforms to “P” gives you a value of type “(() -&gt; Int)?”. You can use ? to optionally call it like so:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func testF&lt;T : P&gt;(t: T) {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;let x = t.f?() // call “f’ if it is available.<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""></div></div></blockquote><div><br class=""></div><span class="Apple-tab-span" style="white-space:pre">        </span>Yes, I agree with you…I didn’t read carefully the answer from Chris. But the implementation is more complex :&nbsp;</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span> Person {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> name:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> surname:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span> PersonRetrievable {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> personDescription : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> loadPerson:(() -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Person</span>)? { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>PersonRetrievable<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> loadPerson:(() -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Person</span>)? { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> GroupPerson : <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">PersonRetrievable</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> personDescription : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>"Hello World"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> loadPerson:(() -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Person</span>)? {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Person</span>(name: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Scrève"</span>, surname: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"David"</span>)}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>Double returns is quite complex to write and understand..but I agree this problem can be solved like this.</div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""><br class=""><br class=""><blockquote type="cite" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>Java has introduced default implementation in interface and it create confusion between classes and interfaces and does not solve the issue I exposed below.<br class=""></blockquote><br class="">We already have default implementations for protocols (via extensions) as well as classes with implementation inheritance, so any related confusion is already there, so I don’t see how the comparison matters.<br class=""></div></div></blockquote><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>Another question is about priority : Is there any guarantee that implementation of protocol will be called instead of extension ? I don’t remembre reading this anywhere.</div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>I think we should keep the actual model by limiting optional keyword to methods, which has really a meaning. <br class=""></blockquote><br class="">I don’t see why you would limit it to methods. There are a number of optional properties in Cocoa[Touch], for example, and it “wrap the result in an extra level of optional” is a reasonable semantic approach here.<br class=""></div></div></blockquote><div><br class=""></div><span class="Apple-tab-span" style="white-space:pre">        </span>I wrote that because properties already have optional behavior with their type.</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Another idea would be to remove the optional keyword and use the same syntax for method with question mark :&nbsp;</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> loadPerson?()</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>And the compiler generates the code with the double return and property.&nbsp;</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>What do you think about this ?</div><div class=""><br class=""></div><div class="">David</div><div class=""><br class=""></div></div></body></html>