<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="">I would suggest a formal proposal because its a bit involved I think. It should ideally match the subtyping rules, so<div class=""><br class=""></div><div class="">- A matches B if A is a subclass of B</div><div class="">- A matches P if A conforms to P</div><div class="">- A matches A?</div><div class="">- A! matches A? and vice versa</div><div class="">- T1 -> U1 matches T2 -> U2 if T2 is a subtype of T1 and U1 is a subtype of U2</div><div class="">- some rules for ‘throws’ functions</div><div class=""><br class=""></div><div class="">I think there might be some tricky cases with associated type inference. We should be careful not to break any existing code.</div><div class=""><br class=""></div><div class="">Also a related proposal would be to allow enum cases to witness static method requirements, and methods to witness property requirements of function type and vice versa, but these seem less useful.</div><div class=""><br class=""></div><div class="">Code for matching witnesses is in TypeCheckProtocol.cpp, and override matching is elsewhere (I think Decl.cpp in lib/AST/)? Take a look if you’re curious about what the rules are today.</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 7, 2016, at 11:06 PM, David Hart 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=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">If it’s an obvious omission, is it better to have it as a bug than a formal proposal?<div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 08 Mar 2016, at 00:16, Joe Groff 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=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Mar 7, 2016, at 8:12 AM, Mark Anders 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 style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: 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><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: 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 id="bloop_customfont" style="margin: 0px;" class="">Consider the following (you can paste it in a Playground to see the error):</div><div id="bloop_customfont" style="margin: 0px;" class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">class</span><span class="Apple-converted-space"> </span><span class="">Node { }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">class</span><span class="Apple-converted-space"> </span><span class="">Containable :</span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">Node</span><span class="">{}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">protocol</span><span class="Apple-converted-space"> </span><span class="">Refers {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class=""> </span><span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">var</span><span class="Apple-converted-space"> </span><span class="">to :</span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">Node</span><span class="Apple-converted-space"> </span><span class="">{</span><span style="color: rgb(187, 44, 162);" class="">get</span><span class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">class</span><span class="Apple-converted-space"> </span><span class="">Link :</span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">Refers</span><span class="Apple-converted-space"> </span><span class="">{</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class=""> </span><span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">var</span><span class="Apple-converted-space"> </span><span class="">to :</span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">Node</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class=""> </span><span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">init</span><span class="">(n :</span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">Node</span><span class="">) {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class=""> </span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">to</span><span class="Apple-converted-space"> </span><span class="">= n</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class=""> }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">class</span><span class="Apple-converted-space"> </span><span class="">Contains :</span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">Refers</span><span class="Apple-converted-space"> </span><span class="">{</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="" class=""> </span><span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">var</span><span class="Apple-converted-space"> </span><span style="" class="">to :</span><span class="Apple-converted-space"> </span><span class="">Containable</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class=""> </span><span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">init</span><span class="">(c :</span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">Containable</span><span class="">) {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class=""> </span><span class="Apple-converted-space"> </span><span style="color: rgb(79, 129, 135);" class="">to</span><span class="Apple-converted-space"> </span><span class="">= c</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class=""> }</span></div><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""></p><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span class="">}</span></div></div><div class=""><span class=""><br class=""></span></div>This currently does not work because it seems that to adopt a protocol, the type of protocol var must match exactly.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: 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 class="">It would be great if objects could be said to adopt a protocol if the type of the var is the type or a derived type. </div><div class="">This would allow me to treat the structure in a type safe way (i.e. only a Containable can have a Contains relationship), </div><div class="">while me to have a set of Refers and iterate through each Node.</div><div class=""><br class=""><div class="">Is there a reason why the type must match exactly? Or could protocols be enhanced to to allow matching</div><div class="">derived types, similar to assignment and func parameter rules? </div></div></div></div></blockquote><div class=""><br class=""></div>No fundamental reason, this just isn't something we've had time to implement. It should be straightforward to support.</div><div class=""><br class=""></div><div class="">-Joe</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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></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=""></div></body></html>