5 ms·
> at the expense of outputting joyfully easy to follow code. I disagree. How is "These are the requirements, oh but some of these are not really required." ea
by Razengan 8y ago
> at the expense of outputting joyfully easy to follow code.
I disagree.
How is "These are the requirements, oh but some of these are not really required." easy to follow?
Swift does a great job of making it easy to make sense of code at the point of use, without having to keep referring back to the definitions of things.
A protocol is a set of requirements. When I see a type adopting a protocol, I know that it must fulfill those requirements. Done. I don't need to check if any of the requirements were optional and I don't need to check if the conforming type implements those optional requirements.
If you need optional requirements (an oxymoron), then just break it into ProtocolA and ProtocolB, using protocol inheritance if needed. That's what the Standard Library does (e.g. with `Collection` and `MutableCollection` etc.) and it works great.
Also, at runtime, I just need to see if `type is ProtocolA` or `type is ProtocolB`. How would you check for the implementation of optional methods within a single protocol, without basically reinventing Objective-C?
Finally, good riddance to header files! May they forever remain entombed along with the semicolons. There are other ways to see the overview of a type. Xcode's Symbol Navigator for one, or the Interface Assistant Editor, or you can put in about the same effort as writing a header file to write good documentation.
- elpakal 8y agoCan't agree more with your disagreement. Also, in regards to documentation - Xcode's new(ish) support for Markdown makes adding code documentation almost an enjoyable experience.
- mapmap 8y agoImagine a protocol for a download: Required 1. Download finishes 2. Download fails Optional 3. Download progress (for updating a progress bar) This seems legit to me. Would one really want the added overhead of having to implement 3 as a separate protocol?
- Razengan 8y agoYou'd present that as `ProgressReporting` or something, because other tasks besides a download would want to report their progress too, and indeed, the Foundation framework already has such a protocol [0]! In your first comment you seem to value the ability to "quickly digest key object interfaces and concepts" but you balk at the added clarity afforded by separating different behaviors into distinct protocols? If by "overhead" you mean a cost to performance, worrying about it at that level falls under premature optimization, until you're certain that the extra protocol noticeably harms performance. If you really insist on having a single protocol, then make `progress` an optional computed property. [0] https://developer.apple.com/documentation/foundation/progressreporting https://developer.apple.com/documentation/foundation/progres... [1] https://developer.apple.com/documentation/foundation/progress https://developer.apple.com/documentation/foundation/progres...