<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 17, 2017, at 2:52 PM, Jonathan Hull via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Out of curiosity, what are the benefits to being able to define that a closure must be pure as a parameter/type definition, as opposed to defining a particular closure to being pure while being passed? What guarantees does it give you as the caller of the closure?</div></div></blockquote><div><br class=""></div><div>If you only accept pure closures and otherwise meet the criteria of a pure function then you are pure. If you have a function like that and want to accept both pure and impure closures and receive the purity of the closure provided then we need syntax indicating something similar to `rethrows`, but for purity.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 16, 2017, at 1:18 PM, T.J. Usiyan <<a href="mailto:griotspeak@gmail.com" class="">griotspeak@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I am ok with a keyword but `pure` in front of func doesn't work well with inline closures.<div class=""><br class=""></div><div class="">A few people talked through many of these issues starting with this tweet. <a href="https://twitter.com/griotspeak/status/832247545325842432" class="">https://twitter.com/griotspeak/status/832247545325842432</a></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, Feb 16, 2017 at 4:13 PM, Jonathan Hull <span dir="ltr" class=""><<a href="mailto:jhull@gbis.com" target="_blank" class="">jhull@gbis.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">+1 for the idea of pure functions in swift. Seems like it would enable a lot of good optimizations (in some cases even just evaluating the function at compile time).<div class=""><br class=""></div><div class="">-1 on the specific notation. I would much rather just put the word ‘pure’ in front of ‘func’, the same way we put ‘mutating' in front of mutating functions… it seems to me like these are part of the same family.</div><div class=""><br class=""></div><div class="">I agree we should allow inout.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On Feb 16, 2017, at 9:03 AM, T.J. Usiyan via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class="m_-8285033497674018801Apple-interchange-newline"></div></div><div class=""><div class=""><div class="h5"><div dir="ltr" class=""><div class=""># Pure Functions</div><div class=""><br class=""></div><div class="">* Proposal: [SE-NNNN](<a href="https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md" target="_blank" class="">https://github.com/<wbr class="">apple/swift-evolution/blob/<wbr class="">master/proposals/NNNN-name.md</a>)</div><div class="">* Author(s): [TJ Usiyan](<a href="https://github.com/griotspeak" target="_blank" class="">https://github.com/<wbr class="">griotspeak</a>)</div><div class="">* Status: **Awaiting review**</div><div class="">* Review manager: TBD</div><div class=""><br class=""></div><div class="">## Introduction</div><div class=""><br class=""></div><div class="">Some functions are, essentially, only meant to be transformations of their input and–as such–do not and should not reference any variables other than those passed in. These same functions are not meant to have any effects other than the aforementioned transformation of input. Currently, Swift cannot assist the developer and confirm that any given function is one of these 'pure' functions. To facilitate this, this proposal adds syntax to signal that a function is 'pure'.</div><div class=""><br class=""></div><div class="">'pure', in this context, means:</div><div class="">1. The function must have a return value</div><div class="">1. This function can only call other pure functions</div><div class="">1. This function cannot access/modify global or static variables.</div><div class=""><br class=""></div><div class="">## Motivation</div><div class=""><br class=""></div><div class="">Consider the following example where `_computeNullability(of:)` is meant to create its output solely based on the provided recognizer.</div><div class=""><br class=""></div><div class="">```</div><div class="">class Recognizer {</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>var nullabilityMemo: Bool?</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>var isNullable: Bool {</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>func _computeNullability(of recognizer: Recognizer) -> Bool {…}</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>if let back = nullabilityMemo {</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>return back<span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span></div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>} else {</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>let back = _computeNullability(of: self)</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>nullabilityMemo = back</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>return back</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>}</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div class="">}</div><div class="">```</div><div class="">if `_computeNullability(of:)` is recursive at all, there exists a real potential to accidentally reference `self` in its body and the mistake, depending on circumstance, can be terribly subtle. Converting `_computeNullability(of:)` to a `static` function is an option but obfuscates the fact that it is *only* to be called within `isNullable`.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">## Proposed solution</div><div class=""><br class=""></div><div class="">Given the ability to indicate that `_computeNullability(of:)` is a 'pure' function, the developer gains assurance from the tooling that it doesn't reference anything or cause any side effects.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">```</div><div class="">class Recognizer {</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>var nullabilityMemo: Bool?</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>var isNullable: Bool {</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>pfunc _computeNullability(of recognizer: Recognizer) -> Bool {…}</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>if let back = nullabilityMemo {</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>return back<span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span></div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>} else {</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>let back = _computeNullability(of: self)</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>nullabilityMemo = back</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>return back</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>}</div><div class=""><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div class="">}</div><div class="">```</div><div class=""><br class=""></div><div class="">## Detailed design</div><div class=""><br class=""></div><div class="">This proposal introduces a new annotation `=>`, which is to be accepted everywhere `->` currently is. Members created using this kewyord must follow the rules listed in the introduction.</div><div class=""><br class=""></div><div class="">## Impact on existing code</div><div class=""><br class=""></div><div class="">This is an additive feature unless alternative 2 is chosen and, as such, should not require an effect on existing code. It could be used to annotate closures accepted by methods in the standard library such as `map`, `filter`, and `reduce`. While this would fit well with their typical use, such a change is not necessarily part of this proposal.</div><div class=""><br class=""></div><div class="">## Alternatives considered</div><div class=""><br class=""></div><div class="">It should be noted that neither of these alternatives can remain consistent for inline closures.</div><div class="">1. keyword `pfunc` (pronounciation: pifəŋk) for 'pure' functions. </div><div class="">2. `proc` keyword for 'impure' functions and 'func' for 'pure' functions. This would be a massively source breaking change and, as such, is unlikely to have any feasibility. It is, however, the most clean semantically, in my opinion.</div><div class=""><br class=""></div></div></div></div><span class="">
______________________________<wbr class="">_________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class=""></span></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>