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 But Cocoa needs this heavily because it relies so much on the delegate pattern. I'm finding (at least in my code) that Swift has much better patterns than delegate. Completion handlers and smaller strategy objects (and bare functions) IMO are better tools in Swift. Cocoa has moved more this way, too. Massive delegates w/ lots of optionals is, I'm coming to believe, indicating a problem rather than something to make more streamlined. 1/

@cocoasamurai This isn't a really strongly-held opinion. I've thought a lot about how I would redesign UITableView and WKWebView without optional-heavy delegates, and I don't have a great answer there. But in day-to-day Swift I'm seeing people reach for delegates in many cases where they shouldn't, so I'm also concerned about creating attractive nuisances and ever-more optionals (as in the `?` kind, which I think are also overused).

I don't really have the answer here.

@cocoaphony yeah really I just want some official way to signal (without documentation or having to see an empty method implementation in a protocol extension) that you don't have to implement a method. I don't do this a lot, so I'd like to keep these methods (required & not) in the same protocol. It's also possible I need to rethink my design a bit to avoid needing the optional methods all together 🤷🏻‍♂️

@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

Fast, secure and up-to-date instance, welcoming everyone around the world. Join us! 🌍

Up since 02/04/2017. ✅

Why should you sign up on

This instance is not focused on any theme or subject, feel free to talk about whatever you want. Although the main language is french, we accept every single language and country.

We're connected to the whole OStatus/ActivityPub fediverse and we do not block any foreign instance nor user.

We do have rules, but the goal is to have responsible users. So far we haven't had any issue with moderation.

The instance uses a powerful server to ensure speed and stability, and it has good uptime.

Instance rapide, sécurisée et à jour, accueillant tout le monde dans le monde entier. Rejoignez-nous ! 🌍

En ligne depuis le 02/04/2017. ✅

Pourquoi devriez-vous vous inscrire sur ?

Cette instance n'est pas centrée sur un thème ou un sujet, n'hésitez pas à parler de ce que vous voulez. Bien que la langue principale soit le français, nous acceptons toutes les langues et tous les pays.

Nous sommes connectés à l'ensemble du fediverse OStatus/ActivityPub et nous ne bloquons aucune autre instance ni aucun utilisateur.

Nous avons des règles, mais l'objectif est d'avoir des utilisateurs responsables. Jusqu'à présent, nous n'avons pas eu de problème avec la modération.

L'instance utilise un serveur puissant pour assurer la vitesse et la stabilité, et elle a un bon uptime.