[swift-evolution] Wouldn't publishing directives (for strong type-aliases) make implicitly-synthesized conformance obsolete? (was: [Accepted] SE-0185 - Synthesizing Equatable and Hashable conformance)

Daryle Walker darylew at mac.com
Fri Aug 18 08:34:53 CDT 2017


> On Aug 17, 2017, at 1:04 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> On Aug 17, 2017, at 5:00 AM, Haravikk via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>>> On 17 Aug 2017, at 11:42, Robert Bennett <rltbennett at icloud.com <mailto:rltbennett at icloud.com>> wrote:
>>> 
>>> Chris mentions that the intent was to mimic a default implementation in a constrained protocol extension, with one extension per type that doesn’t define its own ==/hashValue while conforming to Equatable/Hashable. Constrained extensions are allowed to use type information from the constraint, so I don’t think there is an issue here.
>> 
>> And I disagree; this isn't a constraint extension either, not even close, we're talking here about automatic behaviour based upon variables the protocol knows literally nothing about, in a way that can result in new errors that are currently impossible (as you can't currently conform to Equatable without providing some kind of code to implement it).
> 
> I understand and recognize your concern.  Your points are apply equally to the default implementation of the Codable protocols as well, and the core team specifically discussed this.
> 
> Also, if I were to nitpick your argument a bit, it isn’t true that the protocol knows “nothing" about the type anyway, because the protocol has access to self.  The default implementation could conceptually use reflection to access all the state in the type: we’re producing the same effect with more efficient code.

I’ve proposed having alternative types (strong type-alias / subtypes / quotient types) in the recent past. A key feature is selectively copying the underlying type’s interface:

alter PlagiarizedArray<T>: Array<T>, MutableCollection {

// Copy the interface of Array’s RandomAccessCollection, and forward them to super
publish RandomAccessCollection

/* Don’t do the same for MutableCollection. Either reverse that decision or implement the needed methods (that aren’t already part of RandomAccessCollection) manually. */
//...

}

So, with these new/changed protocols, an alternative that declares conformance to Equatable / Hashable / Encodable / Decodable would effectively get a “publish Whatever” no-so-secretly added, right? That could mess up selective copying of the interface. Of course, both my method and synthesized conformance would just effectively forward to super, so is it a distinction without a difference?

I want alternative types in Swift 5. (They shouldn’t be too hard to implement. I’ve downloaded the compiler’s Git archive to try it out.) Once added, we will have a way to declare implicitly accessible methods, making the synthesized conformance here obsolete. At that point I think we should declare as such and require explicit “publish Equatable/Hashable/Encodable/Decodable/Codable” directives in non-alternatives declaring conformance.

A new problem is that (at least) one version of Swift would have come out by then. Would requiring explicit publishing break backwards compatibility? Could we migrate the code by suggesting to insert the appropriate publishing directives? Could we have a compatibility mode where these five protocols’ implicit synthesizing is grandfathered in, but all new protocols important enough to get this status must need an explicit directive?

— 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 

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


More information about the swift-evolution mailing list