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

Matthew Johnson matthew at anandabits.com
Tue Feb 21 21:05:35 CST 2017

> On Feb 21, 2017, at 8:57 PM, Michel Fortin via swift-evolution <swift-evolution at swift.org> wrote:
> (this was accidentally sent off-list, reposting here a day later)

And this was my reply.

>> 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.

Yep, that’s the kind of thing I had in mind.

I can imagine being this explicit could get pretty cumbersome though.  I wonder if there is a way to streamline this.  For example, in this case if we could just talk about `Element`’s conformance to `Hashable` and we could talk about a `pure` conformance (all requirements are met with pure implementations) then we could just say something like this:

func insert(_ element: Element) { ... }

Note: I also removed some redundancies - we could just say that the parameters are a list of things the purity of which `insert`’s purity depends.

For function arguments it would look something like this:

func map<T>(transform: Element -> T)

> -- 
> Michel Fortin
> https://michelf.ca
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170221/8153fea7/attachment.html>

More information about the swift-evolution mailing list