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

Nicolas Fezans nicolas.fezans at gmail.com
Fri Feb 17 08:57:58 CST 2017


+1 for pure functions
+1 for combining them with constexpr
-1 for the syntax originally proposed
+1 for pure or @pure keywords (before func and before a closure opening { )

One thing is probably worth considering if pure functions and closures are
combined with constexpr and evaluated at compile-time. This might be
problematic if the produced results are large (in size) and in that case it
might be interesting to have the possibility to delay the computation at
run-time... maybe with another keyword then?

Nicolas


On Fri, 17 Feb 2017 at 15:37, Matthew Johnson via swift-evolution <
swift-evolution at swift.org> wrote:

> On Feb 16, 2017, at 3:18 PM, T.J. Usiyan via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> I am ok with a keyword but `pure` in front of func doesn't work well with
> inline closures.
>
>
> The `=>` function arrow syntax is a clever way to avoid making pure
> functions heaver syntactically than impure functions.  That said, I don’t
> think it will stand out very clearly when reading code and is likely to be
> confusing for new programmers who don’t understand purity or why you would
> sometimes want it and other times that it won’t be possible.
>
> Also, what about pure closures that have no need to state a signature
> because it is inferred?  This syntactic sugar is a pretty important aspect
> of Swift and often times some of our smallest closures will be pure.  For
> example Array’s map should be pure when the closure is pure and many map
> closures are very small.  We don’t want to have to annotate these closures
> with a signature.
>
> Could we allow inference of purity for closures when they are used in a
> context which accepts a pure function?  If we had an annotation similar to
> `rethrows` maybe inference could prefer purity, but fall back to an impure
> semantic for `map` (or other methods using the annotation) when the closure
> isn’t pure.  Come to think of it, using `->` vs `=>` to make the
> distinction kind of falls apart when the purity of a function is
> conditional depending on the purity of its arguments.  Have you thought
> about how to handle this?
>
> Overall, I *really* want to see pure functions in Swift and would be very
> excited to see them make it into Swift 4.  That said, I’m on the fence
> about the syntax you have proposed.
>
>
> A few people talked through many of these issues starting with this tweet.
> https://twitter.com/griotspeak/status/832247545325842432
>
> On Thu, Feb 16, 2017 at 4:13 PM, Jonathan Hull <jhull at gbis.com> wrote:
>
> +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).
>
> -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.
>
> I agree we should allow inout.
>
> Thanks,
> Jon
>
> On Feb 16, 2017, at 9:03 AM, 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
> )
> * Author(s): [TJ Usiyan](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
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> 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/aa9d0dcc/attachment.html>


More information about the swift-evolution mailing list