[swift-evolution] [Pitch] Support for pure functions. Part n + 1.

Michel Fortin michel.fortin at michelf.ca
Tue Feb 21 20:57:40 CST 2017

(this was accidentally sent off-list, reposting here a day later)

> Le 20 févr. 2017 à 12:17, Matthew Johnson <matthew at anandabits.com> a écrit :
>> e) Generic Containers:
>> Generic containers that impose requirements on their elements will pose some additional problems, for instance: `Set.insert` needs to call `hashValue` and `==` on its elements, making the purity of `Set.insert` constrained by the purity of those functions. Without a way to express this kind of conditional purity, `Set` and `Dictionary` cannot be pure.
> Could we use a mechanism similar to `rethrows` to address this kind of transitive purity?  We have already discussed something along those lines for handling functions passed as arguments.  Maybe that mechanism could be designed to handle this use case as well.

Similar, yes. But more complicated too. In pseudo code, this is what you'd have to express for `Set.insert`:

	pure(where: Element.hashValue is pure, Element.== is pure)
	func insert(_ element: Element) { ... }

Then the compiler can enforce that `insert` only does things that are allowed in a pure function, but can ignore the purity of Element.hashValue and Element.== because the caller will check for that that.

Michel Fortin

More information about the swift-evolution mailing list