[swift-evolution] Will Swift ever support optional methods without @objc?

Charlie Monroe charlie at charliemonroe.net
Tue Nov 15 02:10:06 CST 2016

> On Nov 15, 2016, at 8:53 AM, Rick Mann <rmann at latencyzero.com> wrote:
>> On Nov 14, 2016, at 22:51 , Charlie Monroe via swift-evolution <swift-evolution at swift.org> wrote:
>> One major example is the NS/UITableViewDataSource or Delegate - there are many many methods that you don't need to implement, hence are optional.
>> But I think that this was partially solved by default implementation of protocol methods, which pretty much does what you want...
> I just realized I only responded to someone else, and not the whole list. It does, but it forces me to make the return value of the protocol method optional, so that the default implementation can return nil. 

You'd still get an optional even with @optional protocol methods:

protocol Foo {
	@optional func bar() -> Int

let x = foo.bar?() // x is Int?

Nevertheless, in the previous example:

> 	var titleForRow:((_ indexPath:IndexPath)->(String))? { get }

You can return String? in the closure instead and the default implementation can simply return a closure that returns nil. This is pretty much equivalent.

> In the end, I guess that's not so bad, since I'm not happy with the entire approach, but it'll do for now.
> -- 
> Rick Mann
> rmann at latencyzero.com

More information about the swift-evolution mailing list