Swift should have a keyword to mark a protocol method as optional, every solution right now feels like a band aid which doesn't fix the problem. @objc isn't great, a protocol extension with an empty implementation is better, but still not great. This feels like Swift should have a keyword to mark a section or individual methods as optional

@cocoasamurai why isn’t a default implementation identical and nicer? Forcing the function itself to be optional (ie requiring optional chaining) feels annoying.

@cocoaphony it accomplishes the desired effect, but it doesn't communicate it very well. ObjC protocols do this very easily at the time of protocol declaration with the optional & required keywords, no further source code or documentation reading is required to understand what you do, or don't have to implement.


@cocoasamurai @cocoaphony A nice alternative would be either:
- An annotation at declaration level to notify that a default implementation exists, for instance `@implemented func foo() -> Bar`
- A required override hint at re-implementation level, so that the dev knows there's a default impl, similar to subclasses and `@override` keyword

@bmt @cocoaphony i'd probably go for a declaration level solution myself. It feels like the problem is a lack of an official annotation to express this concept rather than how it is actually implemented

@cocoasamurai @cocoaphony Yes indeed. The concept is sound, we just miss a piece of information to convey that the method is already implemented (and thus optional), but I wouldn't go back to Obj-C style with optional chaining, respondsToSelector etc.

Sign in to participate in the conversation
Mastodon for Tech Folks

This Mastodon instance is for people interested in technology. Discussions aren't limited to technology, because tech folks shouldn't be limited to technology either!

We adhere to an adapted version of the TootCat Code of Conduct and follow the Toot Café list of blocked instances. Ash is the admin and is supported by Fuzzface as a moderator.

Hosting costs are largely covered by our generous supporters on Patreon – thanks for all the help!