<div dir="ltr">I am ok with a keyword but `pure` in front of func doesn't work well with inline closures.<div><br></div><div>A few people talked through many of these issues starting with this tweet. <a href="https://twitter.com/griotspeak/status/832247545325842432">https://twitter.com/griotspeak/status/832247545325842432</a></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 16, 2017 at 4:13 PM, Jonathan Hull <span dir="ltr"><<a href="mailto:jhull@gbis.com" target="_blank">jhull@gbis.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">+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><br></div><div>-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><br></div><div>I agree we should allow inout.</div><div><br></div><div>Thanks,</div><div>Jon</div><div><br><div><blockquote type="cite"><div><div class="h5"><div>On Feb 16, 2017, at 9:03 AM, T.J. Usiyan via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="m_-8285033497674018801Apple-interchange-newline"></div></div><div><div><div class="h5"><div dir="ltr"><div># Pure Functions</div><div><br></div><div>* Proposal: [SE-NNNN](<a href="https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md" target="_blank">https://github.com/<wbr>apple/swift-evolution/blob/<wbr>master/proposals/NNNN-name.md</a>)</div><div>* Author(s): [TJ Usiyan](<a href="https://github.com/griotspeak" target="_blank">https://github.com/<wbr>griotspeak</a>)</div><div>* Status: **Awaiting review**</div><div>* Review manager: TBD</div><div><br></div><div>## Introduction</div><div><br></div><div>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><br></div><div>'pure', in this context, means:</div><div>1. The function must have a return value</div><div>1. This function can only call other pure functions</div><div>1. This function cannot access/modify global or static variables.</div><div><br></div><div>## Motivation</div><div><br></div><div>Consider the following example where `_computeNullability(of:)` is meant to create its output solely based on the provided recognizer.</div><div><br></div><div>```</div><div>class Recognizer {</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>var nullabilityMemo: Bool?</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>var isNullable: Bool {</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>func _computeNullability(of recognizer: Recognizer) -> Bool {…}</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>if let back = nullabilityMemo {</div><div><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><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>} else {</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>let back = _computeNullability(of: self)</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>nullabilityMemo = back</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>return back</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>}</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div>}</div><div>```</div><div>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><br></div><div><br></div><div>## Proposed solution</div><div><br></div><div>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><br></div><div><br></div><div>```</div><div>class Recognizer {</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>var nullabilityMemo: Bool?</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>var isNullable: Bool {</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>pfunc _computeNullability(of recognizer: Recognizer) -> Bool {…}</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>if let back = nullabilityMemo {</div><div><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><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>} else {</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>let back = _computeNullability(of: self)</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>nullabilityMemo = back</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                        </span>return back</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>}</div><div><span class="m_-8285033497674018801gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div>}</div><div>```</div><div><br></div><div>## Detailed design</div><div><br></div><div>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><br></div><div>## Impact on existing code</div><div><br></div><div>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><br></div><div>## Alternatives considered</div><div><br></div><div>It should be noted that neither of these alternatives can remain consistent for inline closures.</div><div>1. keyword `pfunc` (pronounciation: pifəŋk) for 'pure' functions. </div><div>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><br></div></div></div></div><span class="">
______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></span></div></blockquote></div><br></div></div></blockquote></div><br></div>