> On Oct 16, 2017, at 1:29 PM, Adam Kemp via swift-evolution 
> <swift-evolution@swift.org> wrote:
> 
> 
> 
>> On Oct 16, 2017, at 12:35 PM, Thorsten Seitz via swift-evolution 
>> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>> 
>> IMHO `elementsEqual` provides a nice example for a method which only makes 
>> sense on something meaningfully ordered:
>> What is the use case for `elementsEqual` that works with a Set?
> 
> There may not be one, but here’s the problem:
> 
> 1. It’s generally useful for Set to conform to a protocol that allows you to 
> iterate over its elements.
> 2. It’s generally useful to be able to ask if two objects that you can 
> iterate over are equal by comparing the elements in the order that they’re 
> iterated over.
> 
> The argument being made is that these two protocols should be different, but 
> I don’t think the proponents of that idea have fully thought through what 
> that would mean in practice. Consider a function like map, which takes a 
> Sequence and produces another Sequence. This function is useful for both 
> ordered and unordered elements so it would have to be defined in terms of the 
> looser (unordered) type, which means its output type would be unordered. 
> Imagine starting with an ordered enumerable and calling map on it. What’s the 
> result? An unordered enumerable (the return type of map would be unordered to 
> match its input type). Now you can’t use any of the methods that require the 
> stricter (ordered) protocol on the result, even though you haven’t actually 
> lost the order. You would have to do something to fix up the type and make 
> the compiler see it as ordered again. Maybe there’s an asOrdered method or 
> something. Imagine having to sprinkle that throughout your code just to make 
> things compile. Does that sound like a usable API?
> 
> 
> let people:[Person] = [] // Ordered
> let orderedNames:[String] = [] // Ordered
> let names = people.map { $0.fullName } // Result is unordered
> return names.elementsEqual(orderedNames) // compile error: names is unordered
> // Maybe: return names.asOrdered().elementsEqual(orderedNames)?
> 
> Avoiding this mess would require overloading such that every function that 
> supports either ordered or unordered would have to be written both ways, 
> which would just be a different mess.

That is a great point. 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>
}

?

Or the following; it also requires generalized existentials and recursive 
protocol requirements but not covariance; though I can imagine people arguing 
against introducing more associated types:
 
protocol Unordered {
        associatedtype MapResultType: Unordered
        func map<T>(…) -> Any<M: MapResultType where  M.Element == T>
}

This would even allow an ordered collection to map to an unordered one or 
vice-versa.  Maybe a collection type that spits out an ordered list when 
mapping to Comparables.
(n.b. the more I think about it, the more I like this option. Where are our 
existentials and recursive requirements? :) )

In this case we wouldn't even need an Ordered override; using concrete classes 
directly it would just work, and a function that requires an ordered result 
could specify it as any other constraint just as you do today:

func foo<O: Ordered>(in seq: O) where O.Element == Int, O.MapResultType: 
Ordered {
    let orderedMapping = seq.map { $0 + 1 } // type = Any<O: Ordered where 
O.Element == Int>
    …
}

I'd be ok with `map` and `filter` keeping their array returns (and that's 
what's in my original email about this) until we get the language features 
needed to support these. And I think we'd want to revisit the definitions of 
much of what is currently Sequence once we get these language features anyway.

> 
> All of that would be to solve a problem that in practice doesn’t seem to 
> really cause any problems. I’m not aware of any evidence to suggest that this 
> type causes a significant number of bugs, and we have another 
> language/runtime (C#/.Net) with a large number of active developers and code 
> bases with the same design and the same lack of evidence of a problem.

As I've been saying all along, elementsEqual returning a functionally random 
result when an unordered type is involved is a problem. At the least, we should 
make an OrderedSequence (conforming to Sequence, but could even be otherwise 
empty), move at least elementsEqual and lexicographicallyPrecedes to functions 
or extensions on that, and conform the appropriate stdlib types. Which would 
obviously not include Set or Dictionary. 

> It seems like we’re being asked to make the library significantly harder to 
> work with in order to solve a set of bugs that, as far as I can tell, doesn’t 
> really exist in practice. I think in order to even consider this we would 
> need to see the evidence that there’s a real problem to solve, and see a 
> solution that didn’t make the library significantly harder to use.

My aim for any such changes is to make the library more correct to use with 
little to no breakage of correct code. Done right, I think `Sequence: Iterable` 
can be done with little to no breakage beyond the direct use of Set or 
Dictionary as ordered, which I and others view as incorrect to begin with.

> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to