<div dir="ltr">+1, would love to see UIKit&#39;s (and other frameworks&#39;) delegates/datasources nested like this one day.<div><br></div><div>It seems like there&#39;s nothing that should prevent nesting like this; it&#39;s just going to produce a different mangled name.</div><div><br><br><div class="gmail_quote"><div dir="ltr">On Thu, Apr 28, 2016 at 10:15 AM Brad Hilton via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Type nesting allows some convenient and straightforward semantics that we see inside the Swift standard library such as views on String like String.CharacterView, String.UnicodeScalarView, etc. However a protocol cannot be nested in a type and gives a non-obvious error that the “Declaration is only valid at file scope.” Just as other nested types allow proper contextual scoping, a nested protocol could make a lot sense for a number of patterns. For example, there are many “Delegate” protocols throughout the Cocoa frameworks. Here’s a controller/delegate pattern before and after type nesting:<div><div><br></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span>// Without type nesting</span></div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span><br></span></div><div style="margin:0px;line-height:normal"><span style="color:#bb2ca2">protocol</span><span> MyControllerDelegate : </span><span style="color:#bb2ca2">class</span><span> {</span></div><p style="margin:0px;line-height:normal;min-height:14px"><span>    </span><br></p><div style="margin:0px;line-height:normal"><span>}</span></div><div style="margin:0px;line-height:normal;min-height:14px"><span></span><br></div><div style="margin:0px;line-height:normal"><span style="color:#bb2ca2">class</span><span> MyController {</span></div><p style="margin:0px;line-height:normal;min-height:14px"><span>    </span><br></p><div style="margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="color:#000000">    </span><span style="color:#bb2ca2">weak</span><span style="color:#000000"> </span><span style="color:#bb2ca2">var</span><span style="color:#000000"> delegate: </span><span>MyControllerDelegate</span><span style="color:#000000">?</span></div><p style="margin:0px;line-height:normal;min-height:14px"><span>    </span><br></p><div style="margin:0px;line-height:normal"><span>}</span></div><div style="margin:0px;line-height:normal;min-height:14px"><span></span><br></div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span>// With type nesting</span></div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span><br></span></div><div style="margin:0px;line-height:normal"><span style="color:#bb2ca2">class</span><span> MyController {</span></div><p style="margin:0px;line-height:normal;min-height:14px"><span>    </span><br></p><div style="margin:0px;line-height:normal"><span>    </span><span style="color:#bb2ca2">weak</span><span> </span><span style="color:#bb2ca2">var</span><span> delegate: </span><span style="color:#4f8187">Delegate</span><span>?</span></div><p style="margin:0px;line-height:normal;min-height:14px"><span>    </span><br></p><div style="margin:0px;line-height:normal"><span>    </span><span style="color:#bb2ca2">protocol</span><span> Delegate : </span><span style="color:#bb2ca2">class</span><span> {</span></div><p style="margin:0px;line-height:normal;min-height:14px"><span>        </span><br></p><div style="margin:0px;line-height:normal"><span>    }</span></div><p style="margin:0px;line-height:normal;min-height:14px"><span>    </span><br></p><div style="margin:0px;line-height:normal"><span>}</span></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal"><span style="font-family:Helvetica">Though the change is mostly semantics, it does allow an explicit association between My Controller and the Delegate instead of only a named association. It also cleans up the module name space like other nested types and makes associated protocols more discoverable in my opinion. </span></div><div style="margin:0px;line-height:normal"><span style="font-family:Helvetica"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-family:Helvetica">I’d love to hear everyone’s thoughts.</span></div><div style="margin:0px;line-height:normal"><span style="font-family:Helvetica"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-family:Helvetica">Brad Hilton</span></div></div></div></div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
</blockquote></div></div></div>