<div dir="ltr">-1<div><br></div><div>I think the basic problem is saying the runtime should fail. I feel like a large part of the power of Swift is letting the compiler and the programmer make strong assumptions about types specifically to prevent the runtime from dealing with it.</div><div><br></div><div>In fact, the type system can save us here! I wrote some code you can play with here: <a href="http://swiftstub.com/318278733">http://swiftstub.com/318278733</a></div><div><br></div><div>The basic idea is that if you do something like</div><div><br></div><div> extension FooDelegate where Self: BarController {<br></div><div><div> func didPerformSomeAction(object: Foo) {}</div><div> }</div></div><div><br></div><div>calls to didPerformSomeAction with an argument of type Bar will call the right one for the type! There might be better ways to do that, but that is what immediately came to mind.</div><div><br></div><div dir="ltr"><div><div class="gmail_quote"><div dir="ltr">On Wed, Dec 16, 2015 at 2:43 PM Terrence Katzenbaer via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>Because APIs are designed to be generic, protocols that must be conformed generally use types like Object or other base classes for a given framework. This introduces type casting verbosity when implementing the protocol for a specific use. I would like to see the ability to strengthen argument types in functions declared for protocol conformance.</div><div><br></div><div>An example:</div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#0433ff">class</span> Foo { }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#0433ff">class</span> Bar: <span style="color:#3495af">Foo</span> { }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#0433ff">protocol</span> FooDelegate {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#0433ff">func</span> didPerformSomeAction(object: <span style="color:#3495af">Foo</span>)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#0433ff">class</span> FooController: <span style="color:#3495af">FooDelegate</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#0433ff">func</span> didPerformSomeAction(<span style="color:#0433ff">var</span> object: <span style="color:#3495af">Foo</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:#008f00"><span style="color:#000000"> </span>// I know that object must be a Bar instance</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> object = object <span style="color:#0433ff">as</span>! <span style="color:#3495af">Bar</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:#008f00"><span style="color:#000000"> </span>// do something with object</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:#008f00"><span style="color:#0433ff">class</span><span style="color:#000000"> ProposedFooController: </span><span style="color:#3495af">FooDelegate</span><span style="color:#000000"> { </span>// Type 'ProposedFooController' does not conform to protocol 'FooDelegate'</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#0433ff">func</span> didPerformSomeAction(object: <span style="color:#3495af">Bar</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:#008f00"><span style="color:#000000"> </span>// do something with Bar instance</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p></div><div><br></div><div>The glaring issue I see outright is how the runtime should fail when `didPerformSomeAction` in `ProposedFooController` is called with a non-Bar instance... But perhaps it /should/ fail outright because the programmer has explicitly stated that the type should be Bar.</div><div></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:#eee8e4"></p></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=t5fkNsX-2F7-2FAygTFNPNE08PALgkjkO6I8wpsdssDS0ugoflEETQHfxbRSSUNh46A-2BBnty0AXF7-2F6V5IQrrWPPBPAAl2lhLAQfBmDE5XvduAb6E464TtuafU3IXuGYZPz4mvZzryd8ZWjagjCwHhDshUoA4qyLHUW5EmcqV4f7vbArb-2FwoFZqUuYa9zXycao6i5RWqdYAQSDNHXQd4qkIKr7cbGbIUUbSKIQr58UPkaXc-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</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></div>