[swift-evolution] [Proposal] Make optional protocol methods first class citizens

Sean Heber sean at fifthace.com
Thu Mar 31 13:52:30 CDT 2016


> Caveat: this is going to be strongly-worded; sorry in advance.  I think
> (no offense intended) it's a terrible idea.  The whole notion of an
> “optional requirement” is nonsensical to begin with, and the use of
> optional protocol requirements encourages a style of programming that
> lifts the responsibility of the protocol designer for careful design at
> the expense of clients of the protocol.  There are better ways to do
> things; let's not propagate this anti-pattern any further than it's
> already gone.

This is why my suggestion to solve this required the protocol to include default values for any optional function with a return value - I don’t think that a function should be optional as in “no presence” or even in a way that the caller can identify if the function is or isn’t defined, but just optional as in “it has a clear documented default.” I don’t agree that the caller of an optional protocol function should be able to tell if the function is present or not - the protocol itself should be defined as if all functions are there, but the “optional-ness” is then no worse than default values on function parameters - but that might just be me.

Perhaps a better term for my way of thinking about it would be “default” rather than optional:

protocol UIGestureRecognizerDelegate {
 default func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool = true
 // etc
}

l8r
Sean



More information about the swift-evolution mailing list