<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 11, 2016, at 12:08 AM, David Scrève <<a href="mailto:david.screve@dlta-studio.com" class="">david.screve@dlta-studio.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">Le 11 janv. 2016 à 05:56, Douglas Gregor <<a href="mailto:dgregor@apple.com" class="">dgregor@apple.com</a>> 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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">Hi Chris,<br class=""><br class=""><blockquote type="cite" class="">Le 10 janv. 2016 à 02:29, Chris Lattner <<a href="mailto:clattner@apple.com" class="">clattner@apple.com</a>> 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. 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() -> Int" vs "var f : (() -> Int)? {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><span class="Apple-converted-space"> </span> optional func f() -> 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><span class="Apple-converted-space"> </span> var f: (() -> 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><span class="Apple-converted-space"> </span> var f: (() -> 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 “(() -> 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<T : P>(t: T) {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><span class="Apple-converted-space"> </span> 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 class=""><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 : </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">struct</span><span class="Apple-converted-space"> </span>Person {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space"> </span>name:<span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space"> </span>surname:<span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">protocol</span><span class="Apple-converted-space"> </span>PersonRetrievable {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>personDescription :<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span class="Apple-converted-space"> </span>{<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">get</span><span class="Apple-converted-space"> </span>}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>loadPerson:(() -><span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Person</span>)? {<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">get</span><span class="Apple-converted-space"> </span>}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">extension</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span></span>PersonRetrievable<span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>{</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>loadPerson:(() -><span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Person</span>)? {<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">nil</span><span class="Apple-converted-space"> </span>}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">class</span><span class="Apple-converted-space"> </span>GroupPerson :<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">PersonRetrievable</span><span class="Apple-converted-space"> </span>{</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>personDescription :<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">String</span><span class="Apple-converted-space"> </span>{</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);"><span class="" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">return</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span></span>"Hello World"<span class="" style="font-variant-ligatures: no-common-ligatures;">;</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space"> </span>loadPerson:(() -><span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Person</span>)? {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span>{<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Person</span>(name:<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"Scrève"</span>, surname:<span class="Apple-converted-space"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"David"</span>)}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</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></div></blockquote><div><br class=""></div><div>Right. One could extend the language with a rule that allows a method to satisfy a read-only property requirement when that property is of (possibly optional) function type.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><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></div></blockquote><div><br class=""></div><div>The rules are a little muddy and are being discussed in other threads (e.g., those about dynamic dispatch of members of protocol extensions).</div><br class=""><blockquote type="cite" class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><div class=""><blockquote type="cite" 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.<span class="Apple-converted-space"> </span><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 class=""><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 style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><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 : </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </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. </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></blockquote><br class=""></div><div>Per my first comment about about satisfying a read-only property of function type with a method, I don’t think we need a special syntax at all to make it easy to conform to these protocols.</div><div><br class=""></div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div><br class=""></div><br class=""></body></html>