[swift-evolution] [Pitch] Allow closures/default params to satisfy protocol requirements

Slava Pestov spestov at apple.com
Tue Mar 28 00:31:44 CDT 2017


> On Mar 27, 2017, at 1:48 PM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Assuming we’ll get that functionality, both var and let should satisfy the protocol requirements right?
> 
In theory, there’s no reason not to allow that.

> If it is valid, could we potentially get optional functions in pure Swift as well?
> 
> 
I mean, you can already do this:

protocol P {
  var optionalRequirement: ((Int) -> ())? { get }
}

extension P {
  // “default implementation"
  var optionalRequirement: ((Int) -> ())? { return nil }
}

struct A : P {}

struct B : P {
  let optionalRequirement: ((Int) -> ())? = { (x: Int) in print(x) }
}

But it’s kind of tacky.

If you think about it, “optional requirement” is an oxymoron. What’s your use-case?

Slava
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170327/7c093cdd/attachment.html>


More information about the swift-evolution mailing list