[swift-evolution] [swift-evolution-announce] [Review] SE-0089: Replace protocol<P1, P2> syntax with Any<P1, P2>

Goffredo Marocchi panajev at gmail.com
Mon Jun 13 02:26:30 CDT 2016


Hello Dave,

Sent from my iPhone

> On 13 Jun 2016, at 03:04, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> on Fri Jun 10 2016, Thorsten Seitz <tseitz42-AT-icloud.com> wrote:
> 
>>> Am 09.06.2016 um 19:50 schrieb Thorsten Seitz via swift-evolution <swift-evolution at swift.org>:
>>> 
>>> 
>>>> Am 09.06.2016 um 18:49 schrieb Dave Abrahams via swift-evolution <swift-evolution at swift.org>:
>> 
>>>> 
>>>> on Wed Jun 08 2016, Jordan Rose <swift-evolution at swift.org> wrote:
>>>> 
>>>>>> On Jun 8, 2016, at 13:16, Dave Abrahams via swift-evolution
>>>>>> <swift-evolution at swift.org> wrote:
>>>>>> 
>>>>>> 
>>>>>> on Wed Jun 08 2016, Thorsten Seitz
>>>>> 
>>>>>> <swift-evolution at swift.org
>>>>>> <mailto:swift-evolution at swift.org>>
>>>>>> wrote:
>>>>>> 
>>>>>>> Ah, thanks, I forgot!  I still consider this a bug, though (will have
>>>>>>> to read up again what the reasons are for that behavior).
>>>>>> 
>>>>>> Yes, but in the case of the issue we're discussing, the choices are:
>>>>>> 
>>>>>> 1. Omit from the existential's API any protocol requirements that depend
>>>>>> on Self or associated types, in which case it *can't* conform to
>>>>>> itself because it doesn't fulfill the requirements.
>>>>>> 
>>>>>> 2. Erase type relationships and trap at runtime when they don't line up.
>>>>>> 
>>>>>> Matthew has been arguing against #2, but you can't “fix the bug” without
>>>>>> it.
>>>>> 
>>>>> #1 has been my preference for a while as well, at least as a starting
>>>>> point.
>>>> 
>>>> I should point out that with the resyntaxing of existentials to
>>>> Any<Protocols...>, the idea that Collection's existential doesn't
>>>> conform to Collection becomes far less absurd than it was, so maybe this
>>>> is not so bad.
>>> 
>>> I think the problem is more that Any<Collection> does not conform to
>>> a specific value for a type parameter T: Collection
>>> 
>>> What I mean by this is that `Collection` denotes a type family, a
>>> generic parameter `T: Collection` denotes a specific (though
>>> unknown) member of that type family and `Any<Collection>` denotes
>>> the type family again, so there is really no point in writing
>>> Any<Collection> IMO.
>>> The type family cannot conform to T because T is just one fixed member of it.
>>> It conforms to itself, though, as I can write
>>> let c1: Any<Collection> = …
>>> let c2: Any<Collection> = c1
>>> 
>>> That’s why I think that we could just drop Any<Collection> and simply write Collection.
>> 
>> Let me expand that a bit:
>> 
>> Actually all this talk about existentials vs. generics or protocols
>> vs. classes has had me confused somewhat and I think there are still
>> some misconceptions present on this list sometimes, so I’ll try to
>> clear them up:
> 
> There are several objectively incorrect statements here, and several
> others with which I disagree.  I was hoping someone else would write
> this for me, but since the post has such a tone of authority I feel I
> must respond.
> 
>> (1) misconception: protocols with associated types are somehow very
>> different from generics
>> 
>> I don’t think they are and I will explain why. The only difference is
>> the way the type parameters are bound: generics use explicit parameter
>> lists whereas protocols use inheritance. That has some advantages
>> (think long parameter lists of generics) and some disadvantages.
>> These ways are dual in a notation sense: generic types have to have
>> all parameters bound whereas protocols cannot bind any of them.
>> The „existential“ notation `Any<>` being discussed on this list is
>> nothing more than adding the ability to protocols to bind the
>> parameters to be used just like Java’s wildcards are adding the
>> opposite feature to generics, namely not having to bind all
>> parameters.
> 
> Protocols and generics fulfill completely different roles in Swift, and
> so, **especially in a language design context like the one we're in
> here**, must be thought of differently.  The former are an abstraction
> mechanism for APIs, and the latter a mechanism for generalizing
> implementations.  The only place you could argue that they intersect is
> in generic non-final classes, because a class fills the dual role of
> abstraction and implementation mechanism (and some might say that's a
> weakness).  But even accounting for generic classes, protocols with
> associated types are very different from generics.  Two utterly
> different types (an enum and a struct, for example) can conform to any
> given protocol P, but generic types always share a common basis
> implementation.  There is no way to produce distinct instances of a
> generic type with all its type parameters bound, but for any protocol P
> I can make infinitely many instances of P with P.AssociatedType == Int.
> 
> Back to the my original point: while protocols and generic types have
> some similarities, the idea that they are fundamentally the same thing
> (I know you didn't say *exactly* that, but I think it will be read that
> way) would be wrong and a very unproductive way to approach language
> evolution.
> 
>> Essentially `Any<Collection>` in Swift is just the same as
>> `Collection<?>` in Java (assuming for comparability’s sake that
>> Swift’s Collection had no additional associated types; otherwise I
>> would just have to introduce a Collection<Element, Index> in Java).
> 
> I don't see how you can use an example that requires *assuming away*
> assoociated types to justify an argument that protocols *with associated
> types* are the same as generics.
> 
>> Likewise `Any<Collection where .Element: Number>` is just the same as
>> `Collection<? extends Number>` in Java.
>> 
>> And just like Collection<?> does not conform to a type parameter `T
>> extends Collection<?>` because Collection<?> is the type `forall
>> E. Collection<E>` whereas `T extends Collection<?>` is the type
>> `T. Collection<T>` for a given T.
>> 
>> In essence protocols with associated types are like generics with
>> wildcards.
> 
> It is true that generics with wildcards in Java *are* (not just “like”)
> existential types but I don't agree with the statement above.  Because
> Java tries to create an “everything is a class” world, generic classes
> with bound type parameters end up playing the role of existential type.
> But protocols in Swift are not, fundamentally, just existential types,
> and the resyntaxing of ProtocolName to Any<ProtocolName> for use in type
> context is a huge leap forward in making that distinction clear... when
> that's done (unless we leave Array<ProtocolName> around as a synonym for
> Array<Any<ProtocolName>>—I really hope we won't!)  protocols indeed
> *won't* be types at all, existential or otherwise.
> 
>> Coming back to the questions whether (a) allowing existentials to be
>> used as types is useful
> 
> That's the only use existentials have.  They *are* types.  Of course
> they're useful, and I don't think anyone was arguing otherwise.
> 
>> and (b) whether sacrificing type safety would somehow be necessary for
>> that, I think we can safely answer (a) yes, it *is* useful to be able
>> to use existentials like Any<Collection> as types, because wildcards
>> are quite often needed and very useful in Java (they haven’t been
>> added without a reason) (b) no, sacrificing type safety does not make
>> sense, as the experience with Java’s wildcards shows that this is not
>> needed.
> 
> I would call this “interesting information,” but hardly conclusive.
> Java's generics are almost exactly the same thing as Objective-C
> lightweight generics, which are less capable and less expressive in
> many ways than Swift's generics.  
> 
>> Especially if something like path dependent types is used like
>> proposed and some notation to open an existential’s type is added,
>> which is both something that Java does not have.
>> 
>> (2) misconception: POP is different from OOP
>> 
>> It is not. Protocols are just interfaces using subtyping like OOP has
>> always done. They just use associated types instead of explicit type
>> parameters for generics (see above).
> 
> They are not the same thing at all (see above ;->).  To add to the list
> above, protocols can express fundamental relationships—like Self
> requirements—that OOP simply can't handle.  There's a reason Java can't
> express Comparable without losing static type-safety.  Finally, in a
> language with first-class value types, taking a protocol-oriented
> approach to abstraction leads to *fundamentally* different designs from
> what you get using OOP.
> 
>> The more important distinction of Swift is emphasizing value types and
>> making mutation safely available by enforcing copy semantics for value
>> types.  
> 
> We don't, in fact, enforce copy semantics for value types.  

It was again clarified that Copy on Write was only automatically added for Strings and Arrays, and subclasses I suppose, but how else are structs passed around if not by copy? If they are passed around by reference and people are starting, being encouraged to do so, to massively make use of structs... will enforcing copy semantics (even with opt-in CoW behaviour) be a massive cause of performance regression in games and similar kinds of tight processing loops? 

> That's
> something I'd like to change.  But regardless, value types would be a
> *lot* less useful if they couldn't conform to protocols, and so they
> would be a lot less used.  Heck, before we got protocol extensions in
> Swift 2, there was basically *no way* to share implementation among
> value types.  

Ah so it is their (value types) fault our beautiful abstract API contracts meant to decouple behaviour from implementation now contain code and this implementation ;)?

The other day I was revisiting the rationale by Java engineers for default methods in Java interfaces, which I always used to compare Objective-C protocols to, and beside from the ease of adding code to a lot of pre-existing objects without forcing developers using a library to have to do much to adapt to a new update of said library... Well, I did not actually find much beyond convenience. 
In Swift, they are already enabling structs to do something you could have only done with classes before (sharing implementation): in a way it is kind of forcing people to use composition over subclassing without as much effort as before.

Still, I appreciated how clear dispatching/overriding rules were designed (although they may be limiting for some use cases people here might conceive):
> When you extend an interface that contains a default method, you can do the following:
> * Not mention the default method at all, which lets your extended interface inherit the default method.
> * Redeclare the default method, which makes it abstract.
> * Redefine the default method, which overrides it.
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

I find that quite intuitive to adapt to. What I would like to ask you is this: don't you feel it is dangerous or at least unintuitive that casting generally does not allow to change how the object behaves (it does not interfere with the object's implementation of its API contract) while if you are using default methods in protocol extensions you are suddenly allowing casting to change the class implementation?

Are extensions and in particular default methods meant more for value types, implementation sharing too as you were saying, than classes?

> So you can't take protocols out of the picture without
> making value types, and the argument for value semantics, far weaker.
> 
>> But protocols are not really different from interfaces in Java. 
>> I would have preferred a unified model using just classes with real
>> multiple inheritance like Eiffel has and value types just being a part
>> of that similar to Eiffel’s `expanded` classes. But that ship has
>> probably sailed a long time ago :-/ So be it. But at least there
>> should be no reasons for POP vs OOP wars ;-) (I’d like to add that I
>> liked Dave’s talks at last WWDC very much, it’s just that I don’t
>> think that POP is something new or different.)
> 
> Protocol-oriented programming is about the synergy of features and ideas
> most of which not *individually* new, but that together create a new
> world of possibilities.  I've already discussed the synergy of protocols
> and first-class value semantics.  There's also the fact that in
> protocols we have one construct with which to express dynamic
> polymorphism (existentials) and static polymorphism (generic
> constraints), both of which have important roles to play but that I
> maintain are very different indeed.  One result is that you can “start
> with a protocol” as your abstraction mechanism and know that you're not
> going to design yourself into a corner where it becomes impossibly
> awkward to express what you need.  Finally—and I'm certain this *is*
> new—in protocol extensions we have a means to express both post-hoc
> conformance and generic functions that is much more accessible to users
> than in any previous language, to the point where generic programming
> can become a natural part of everyday work.
> 
> -- 
> Dave
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160613/60025814/attachment.html>


More information about the swift-evolution mailing list