[swift-evolution] [Draft] Rename Sequence.elementsEqual
Adam Kemp
adam_kemp at apple.com
Tue Oct 17 12:54:34 CDT 2017
> On Oct 17, 2017, at 10:41 AM, Kevin Nattinger via swift-evolution <swift-evolution at swift.org> wrote:
>
>> On Oct 17, 2017, at 10:36 AM, Adam Kemp <adam_kemp at apple.com <mailto:adam_kemp at apple.com>> wrote:
>>
>>
>>> On Oct 17, 2017, at 10:00 AM, Kevin Nattinger <swift at nattinger.net <mailto:swift at nattinger.net>> wrote:
>>>
>>> Once we allow covariant functions to satisfy protocol requirements and have generalized existentials and recursive protocol requirements, wouldn't we be able to update thusly:
>>>
>>> protocol Unordered {
>>> func map<T>(…) -> Any<U: Unordered where U.Element == T>
>>> }
>>> protocol Ordered: Unordered {
>>> func map<T>(…) -> Any<O: Ordered where O.Element == T>
>>> }
>>>
>>
>> Now apply that to every order-preserving function that takes a Sequence and returns another Sequence. You’ve moved the burden from users of API to implementers of API. It reminds me of the const/non-const split that C++ developers have to deal with, where a lot of functions end up being implemented twice so that you can have a const version and a non-const version (generally one just calls the other). It’s a pain. I don’t want that when working with Sequences. I don’t think it’s worth it. And FWIW, when I was programming in C# I wrote functions that took an IEnumerable<T> and return another IEnumerable<T> very often. It’s a powerful feature that would have been ruined by a change like this.
>
> The idea is that covariance would mean you only need to implement the function once.
In the example you showed above map is written twice. Maybe the two protocols can share an implementation, but you still have to have two versions declared somewhere.
What does it look like if you’re just writing a single function somewhere that takes in a Sequence and returns another Sequence? How do you make that function take both ordered and unordered Sequences? To make it concrete, say you write a function that just wraps map:
func firstNames(ofPeople people: Sequence<Person>) -> Sequence<Person> {
return people.map { $0.firstName }
}
I want that function to work on both ordered and unordered Sequences, and if you start with an ordered Sequence you should end up with an ordered Sequence. How do I make that work?
> If there are no real-world problems, why do we feel the need to change the function name in the first place?
To quote myself from an earlier email: "I’m not even sure a name change is necessary for this method at all, but I’m not at all in favor of anything beyond that.”
To extend that, I’m content with doing nothing. I’m not sure there’s cause for doing anything at all, and I’m very sure that no one on this list has demonstrated any need for a major change to the library, let alone new language features.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171017/0f49fab5/attachment.html>
More information about the swift-evolution
mailing list