<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">https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md</a>)</div><div>* Author(s): [TJ Usiyan](<a href="https://github.com/griotspeak">https://github.com/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 &#39;pure&#39; functions. To facilitate this, this proposal adds syntax to signal that a function is &#39;pure&#39;.</div><div><br></div><div>&#39;pure&#39;, 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="gmail-Apple-tab-span" style="white-space:pre">        </span>var nullabilityMemo: Bool?</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>var isNullable: Bool {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>func _computeNullability(of recognizer: Recognizer) -&gt; Bool {…}</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>if let back = nullabilityMemo {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>return back<span class="gmail-Apple-tab-span" style="white-space:pre">                </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>} else {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>let back =  _computeNullability(of: self)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>nullabilityMemo = back</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>return back</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </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 &#39;pure&#39; function, the developer gains assurance from the tooling that it doesn&#39;t reference anything or cause any side effects.</div><div><br></div><div><br></div><div>```</div><div>class Recognizer {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>var nullabilityMemo: Bool?</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>var isNullable: Bool {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>pfunc _computeNullability(of recognizer: Recognizer) -&gt; Bool {…}</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>if let back = nullabilityMemo {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>return back<span class="gmail-Apple-tab-span" style="white-space:pre">                </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>} else {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>let back =  _computeNullability(of: self)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>nullabilityMemo = back</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>return back</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>}</div><div>}</div><div>```</div><div><br></div><div>## Detailed design</div><div><br></div><div>This proposal introduces a new annotation `=&gt;`, which is to be accepted everywhere `-&gt;` 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 &#39;pure&#39; functions. </div><div>2. `proc` keyword for &#39;impure&#39; functions and &#39;func&#39; for &#39;pure&#39; 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>