Wednesday, October 11, 2017

HashVisitable Swift Evolution Proposal

Vincent Esche (via Tyler Fox):

In short: Hashable is utterly error-prone and does not compose well. And that’s a shame!

[…]

If even the official Swift documentation gets the implementation of hashValue wrong, then who is to expect the average Swift programmer to do any better?

[…]

Now, I’m the first to admit that a scenario where one needs to have multiple hashing algorithms is rather rare.

There are however certain circumstances where multiple hashes per value are desired, if not outright necessary: Bloom Filters.

[…]

As the name implies this new API makes use of the visitor pattern. Instead of implementing the hashing logic on the type T itself we are moving it into dedicated Hasher types, which then get passed to the objects to be hashed.

Here’s his propoasal.

See also: SE-0185: Synthesizing Equatable and Hashable conformance.

Comments RSS · Twitter

Leave a Comment