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

Haravikk swift-evolution at haravikk.me
Fri Feb 17 05:14:00 CST 2017


I like the idea of having pure functions in Swift, but my first thought is; should we have to declare it at all? Is it not easier to just have the compiler automatically flag a function as pure or not?

With that in mind we don't need any new syntax, but a simple @pure attribute should be sufficient. This can be used anywhere that a function is declared, or a closure is accepted as a parameter, allowing us to be explicit that we are trying to define a pure function, or only accept pure closures.

The big benefit of this is that it is retroactive; all existing functions that are pure will be automatically detected as such, and can be passed into any method accepting only pure functions. The new capability will be that developers can specify that a function *must* be pure and thus produce an error if it isn't.

> On 16 Feb 2017, at 17:03, T.J. Usiyan via swift-evolution <swift-evolution at swift.org> wrote:
> 
> # Pure Functions
> 
> * Proposal: [SE-NNNN](https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md <https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md>)
> * Author(s): [TJ Usiyan](https://github.com/griotspeak <https://github.com/griotspeak>)
> * Status: **Awaiting review**
> * Review manager: TBD
> 
> ## Introduction
> 
> 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'.
> 
> 'pure', in this context, means:
> 1. The function must have a return value
> 1. This function can only call other pure functions
> 1. This function cannot access/modify global or static variables.
> 
> ## Motivation
> 
> Consider the following example where `_computeNullability(of:)` is meant to create its output solely based on the provided recognizer.
> 
> ```
> class Recognizer {
> 	var nullabilityMemo: Bool?
> 	var isNullable: Bool {
> 		func _computeNullability(of recognizer: Recognizer) -> Bool {…}
> 		if let back = nullabilityMemo {
> 			return back		
> 		} else {
> 			let back =  _computeNullability(of: self)
> 			nullabilityMemo = back
> 			return back
> 		}
> 	}
> }
> ```
> 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`.
> 
> 
> ## Proposed solution
> 
> 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.
> 
> 
> ```
> class Recognizer {
> 	var nullabilityMemo: Bool?
> 	var isNullable: Bool {
> 		pfunc _computeNullability(of recognizer: Recognizer) -> Bool {…}
> 		if let back = nullabilityMemo {
> 			return back		
> 		} else {
> 			let back =  _computeNullability(of: self)
> 			nullabilityMemo = back
> 			return back
> 		}
> 	}
> }
> ```
> 
> ## Detailed design
> 
> 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.
> 
> ## Impact on existing code
> 
> 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.
> 
> ## Alternatives considered
> 
> It should be noted that neither of these alternatives can remain consistent for inline closures.
> 1. keyword `pfunc` (pronounciation: pifəŋk) for 'pure' functions. 
> 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.
> 
> _______________________________________________
> 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/20170217/67d3543a/attachment.html>


More information about the swift-evolution mailing list