[swift-evolution] [Proposal] Explicit Non-Default-Implemented Protocol Requirements

Gor Gyolchanyan gor.f.gyolchanyan at icloud.com
Wed Aug 2 06:09:36 CDT 2017


> On Aug 2, 2017, at 1:26 PM, Tino Heth <2th at gmx.de> wrote:
> 
> 
>> That would work as well, but it has the downside of forcing a potentially huge number of methods to be implemented in a single place, reducing the readability as opposed to packing them into semantically related groups in the form of extensions.
> I really don't get why people are so obsessed with same-file extensions:

The same-file mentality comes from Swift 3, which introduced fileprivate (which, since Swift 4, got merged into private within a single file scope). With that feature, implementors don't have to choose between access protection and code locality and can get both.

> They are recommended in style guides, influencers blog about them, and they motivated a ridiculous complex change in the access rights system. Yet I haven't seen any evidence that they offer real benefit.

Extensions are a tool for decentralizing code. There are some critical limits on extensions that make main type definition subject to unavoidable bloating (required and designated initializers, stored properties, the deinitializer, and open methods), but everything else is a prime candidate for decentralizing. Putting as little code as possible into the type definition and semantically grouping the implementation into extensions improves readability and maintainability dramatically.

> Extensions are great for adding useful helpers to existing types, and still allow you to selectively expose details of your own classes — but most people seem to ignore those options and focus on something can be done better with plain old comments.

Relying on comments for invariants and preconditions is a poor design decision, because there's no way of enforcing them and the whole integrity of the code is thrown at the mercy of a human's carefulness (which is a horrible fate to befall upon any code). By writing the code in such a way that makes it impossible to be misused (by way of compiler enforcement), the code becomes resilient and no amount of clumsy usage can lead to incredibly obscure bugs that would take a week of debugging to catch.

> [sorry for the rant — but I think a critical look at extensions is long overdue: I rarely see someone questioning their role, so basically, we are making important decisions based on pure superstition]
> 
> A protocol itself is already a vehicle to group related methods,

A protocol is a vehicle for generic programming and separation of abstractions.

>  and if you have a huge entity, it doesn't get better just because you split it and hide its complexity.

Splitting and hiding complexity is by far the only reasonable way of dealing with huge entities. If the entity gains too much responsibility, it's probably a good idea to split it into several smaller entities. If the entity contains a large amount of accidental complexity that solely serves the purpose of enabling a select set of intended features, then it's probably a good idea to hide the accidental complexity away from users of the entity.

In fact, that's exactly why I always wished that protocols could get private requirements that are there solely for use in protocol extensions and are otherwise hidden from existence. I haven't talked about this in detail because I don't see a reasonable way of implementing it yet.

>> Also, please include the original message for reference purposes.
> [hopes Discourse will happen soon ;-) ]

Original Post: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170731/038457.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170731/038457.html>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170802/c7b4442f/attachment.html>


More information about the swift-evolution mailing list