[swift-evolution] Idea: "pure" keyword for function signatures

Michael Savich savichmichael at icloud.com
Fri May 26 05:55:02 CDT 2017


The breakdown of purity in D is very interesting. In D, a pointer that is marked as constant means the memory it points to is considered constant. One of the issues raised is that Swift does not have that “transitive immutability”, which limits what the compiler can infer. I can see how that would be tricky. 

But is supporting reference types really necessary here? You can get a lot done in Swift using only structs, enums, and protocols, which all support value semantics. If we want to have a pure function act on the values in a reference type, you can just write "someObject.member = someFunc(someObject.member)"

There’s also the split opinions on whether this keyword would be most useful for communicating intent to the reader of the code, or performing compiler optimizations. I’m very firmly in the former camp. From my point of view compiler optimizations are just gravy.

There are also syntactic disagreements, but as someone mentioned, getting too attached to a particular syntax is probably just bikeshedding at this stage.

Sent from my Mac

> On May 25, 2017, at 6:57 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
> This is a topic of considerable history. See:
> 
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/003684.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/003684.html>
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/003900.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/003900.html>
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/006005.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/006005.html>
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160905/027010.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160905/027010.html>
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170213/032076.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170213/032076.html>
> 
> It would be important for those who wish to rekindle this discussion first to review and summarize the preceding, and very technically illuminating, discussions.
> 
> 
> On Thu, May 25, 2017 at 5:39 PM, Michael Savich via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Writing functions without side effects is generally considered to result in less error-prone code. In Swift today, if you want to segment your code into pure and impure functions, you just have to police yourself, which is a very un-Swifty thing to have to do. This problem is compounded when working in teams, where someone else of course won’t know which of your functions are pure, and even if you leave a comment it’s not a guarantee they’ll know (or care) what “pure” means.
> 
> So what about adding the ability to annotate functions with a special keyword? For example "pure func addTwoNums(n1: Int, n2: Int)”.
> The rule here is very simple: functions annotated with “pure” can only call other functions annotated with “pure”, otherwise the compiler produces an error.
> 
> To me, this feels like a very natural fit for Swift. What does everybody else think?
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170526/257a1b6e/attachment.html>


More information about the swift-evolution mailing list