<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="">Hi all,<div class=""><br class=""></div><div class="">Optional protocol requirements in Swift have the restriction that they only work in @objc protocols, a topic that’s come up a <a href="http://thread.gmane.org/gmane.comp.lang.swift.devel/1316/focus=8804" class="">number</a> of <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/13347/focus=13480" class="">times</a>. The start of these threads imply that optional requirements should be available for all protocols in Swift. While this direction is implementable, each time this is discussed there is significant feedback that optional requirements are not a feature we want in Swift. They overlap almost completely with default implementations of protocol requirements, which is a more general feature, and people seem to feel that designs based around default implementations and refactoring of protocol hierarchies are overall better.</div><div class=""><br class=""></div><div class="">The main concern with removing optional requirements from Swift is their impact on Cocoa: Objective-C protocols, especially for delegates and data sources, make heavy use of optional requirements. Moreover, there are no default implementations for any of these optional requirements: each caller effectively checks for the presence of the method explicitly, and implements its own logic if the method isn’t there.</div><div class=""><br class=""></div><div class=""><b class="">A Non-Workable Solution: Import as optional property requirements</b></div><div class="">One suggestion that’s come up to map an optional requirement to a property with optional type, were “nil” indicates that the requirement was not satisfied. For example, </div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">@protocol NSTableViewDelegate</font></div><div class=""><font face="Menlo" class="">@optional</font></div><div class=""><font face="Menlo" class="">- (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;</font></div><div class=""><font face="Menlo" class="">- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;</font></div><div class=""><font face="Menlo" class="">@end</font></div></blockquote><div class=""><br class=""></div><div class="">currently comes in as</div><div class=""><br class=""></div><div class=""><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">@objc protocol NSTableViewDelegate {</font></div><div class=""><font face="Menlo" class=""> optional func tableView(_: NSTableView, viewFor: NSTableColumn, row: Int) -> NSView?</font></div><div class=""><font face="Menlo" class=""> optional func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div></blockquote></div><div class="">would come in as:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">@objc protocol NSTableViewDelegate {</font></div><div class=""><font face="Menlo" class=""> var tableView: ((NSTableView, viewFor: NSTableColumn, row: Int) -> NSView?)? { get }</font></div><div class=""><font face="Menlo" class=""> var tableView: ((NSTableView, heightOfRow: Int) -> CGFloat)? { get }</font></div><div class=""><font face="Menlo" class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">with a default implementation of “nil” for each. However, this isn’t practical for a number of reasons:</div><div class=""><br class=""></div><div class="">a) We would end up overloading the property name “tableView” a couple dozen times, which doesn’t actually work.</div><div class=""><br class=""></div><div class="">b) You can no longer refer to the member with a compound name, e.g., “delegate.tableView(_:viewFor:row:)” no longer works, because the name of the property is “tableView”.</div><div class=""><br class=""></div><div class="">c) Implementers of the protocol now need to provide a read-only property that returns a closure. So instead of</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">class MyDelegate : NSTableViewDelegate {</font></div><div class=""><font face="Menlo" class=""> func tableView(_: NSTableView, viewFor: NSTableColumn, row: Int) -> NSView? { … }</font></div><div class=""><font face="Menlo" class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">one would have to write something like</div><div class=""><br class=""></div><div class=""><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">class MyDelegate : NSTableViewDelegate {</font></div><div class=""><font face="Menlo" class=""> var tableView: </font><span style="font-family: Menlo;" class="">((NSTableView, viewFor: NSTableColumn, row: Int) -> NSView?)? = {</span></div><div class=""><span style="font-family: Menlo;" class=""> </span><font face="Menlo" class="">… except you can’t refer to self in here unless you make it lazy ...</font></div><div class=""><span style="font-family: Menlo;" class=""> }</span></div><div class=""><span style="font-family: Menlo;" class="">}</span></div></blockquote></div><div class=""><br class=""></div><div class="">d) We’ve seriously considered eliminating argument labels on function types, because they’re a complexity in the type system that doesn’t serve much of a purpose.</div><div class=""><br class=""></div><div class="">One could perhaps work around (a), (b), and (d) by allowing compound (function-like) names like tableView(_:viewFor:row:) for properties, and work around (c) by allowing a method to satisfy the requirement for a read-only property, but at this point you’ve invented more language hacks than the existing @objc-only optional requirements. So, I don’t think there is a solution here.</div><div class=""><br class=""></div><div class=""><b class="">Proposed Solution: Caller-side default implementations</b></div><div class=""><br class=""></div><div class="">Default implementations and optional requirements differ most on the caller side. For example, let’s use NSTableView delegate as it’s imported today:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">func useDelegate(delegate: NSTableViewDelegate) {</font></div><div class=""><font face="Menlo" class=""> if let getView = delegate.tableView(_:viewFor:row:) { // since the requirement is optional, a reference to the method produces a value of optional function type</font></div><div class=""><font face="Menlo" class=""> // I can call getView here</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""> if let getHeight = delegate.tableView(_:heightOfRow:) {</font></div><div class=""><font face="Menlo" class=""> // I can call getHeight here</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><span style="font-family: Menlo;" class="">}</span></div></blockquote><div class=""><br class=""></div><div class="">With my proposal, we’d have some compiler-synthesized attribute (let’s call it @__caller_default_implementation) that gets places on Objective-C optional requirements when they get imported, e.g.,</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">@objc protocol NSTableViewDelegate {</font></div><div class=""><font face="Menlo" class=""> @__caller_default_implementation func tableView(_: NSTableView, viewFor: NSTableColumn, row: Int) -> NSView?</font></div><div class=""><font face="Menlo" class=""> </font><span style="font-family: Menlo;" class="">@__caller_default_implementation</span><font face="Menlo" class=""> func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat</font></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">And “optional” disappears from the language. Now, there’s no optionality left, so our useDelegate example tries to just do correct calls:</div><div class=""><br class=""></div><div class=""><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">func useDelegate(delegate: NSTableViewDelegate) -> NSView? {</font></div><div class=""><font face="Menlo" class=""> let view = delegate.tableView(tableView, viewFor: column, row: row)</font></div><div class=""><font face="Menlo" class=""> let height = delegate.tableView(tableView, heightOfRow: row)</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div></blockquote></div><div class="">Of course, the code above will fail if the actual delegate doesn’t implement both methods. We need some kind of default implementation to fall back on in that case. I propose that the code above produce a compiler error on both lines *unless* there is a “default implementation” visible. So, to make the code above compile without error, one would have to add:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">extension NSTableViewDelegate {</font></div><div class=""><font face="Menlo" class=""> @nonobjc func tableView(_: NSTableView, viewFor: NSTableColumn, row: Int) -> NSView? { return nil }</font></div><div class=""><font face="Menlo" class=""> </font></div><span style="font-family: Menlo;" class=""> @nonobjc </span><span style="font-family: Menlo;" class="">func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat { return 17 }</span><div class=""><font face="Menlo" class="">} </font></div></blockquote><div class=""><br class=""></div><div class="">Now, the useDelegate example compiles. If the actual delegate implements the optional requirement, we’ll use that implementation. Otherwise, the caller will use the default (Swift-only) implementation it sees. From an implementation standpoint, the compiler would effectively produce the following for the first of these calls:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class="">if delegate.responds(to: #selector(NSTableViewDelegate.tableView(_:viewFor:row:))) {</font></div><div class=""><font face="Menlo" class=""> // call the @objc instance method with the selector tableView:viewForTableColumn:row:</font></div><div class=""><font face="Menlo" class="">} else {</font></div><div class=""><font face="Menlo" class=""> // call the Swift-only implementation of tableView(_:viewFor:row:) in the protocol extension above</font></div><div class=""><font face="Menlo" class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">There are a number of reasons why I like this approach:</div><div class=""><br class=""></div><div class="">1) It eliminates the notion of ‘optional’ requirements from the language. For classes that are adopting the NSTableViewDelegate protocol, it is as if these requirements had default implementations.</div><div class=""><br class=""></div><div class="">2) Only the callers to these requirements have to deal with the lack of default implementations. This was already the case for optional requirements, so it’s not an extra burden in principle, and it’s generally going to be easier to write one defaulted implementation than deal with it in several different places. Additionally, most of these callers are probably in the Cocoa frameworks, not application code, so the overall impact should be small.</div><div class=""><br class=""></div><div class="">Thoughts?</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div class=""><br class=""></div></body></html>