[swift-evolution] protocol can only be used as a generic constraint because it has Self or associated type requirements

Martin Kühl martin.kuehl at gmail.com
Mon Dec 14 09:26:16 CST 2015


On 14 December 2015 at 14:20, Marc Knaup via swift-evolution
<swift-evolution at swift.org> wrote:
>
> I find the suggestion from Paul Cantrell very useful where you can use all properties and methods of the protocol which do not depend on an associated type or Self.
>
> var array = [Hashable]()
> // …
> for element in array {
>     print(element.hashValue)
> }
>
> Who cares that it doesn't fulfill Equatable conformance yet?
>

Exactly. This would let us write a function like

    func hash(Hashable... values) -> Int {
        // calculate combined hashValue from all hashable values
    }

analogous to Guava's Objects.hashCode to help implement hashValue in
custom structs.

Martin


More information about the swift-evolution mailing list