<html><head></head><body>I'm not yet really familiar with this term but correct me if I'm wrong, "pure" (function wise) means functions won't have access to global or static, and mutable variables through their arguments, right?<br><br><br><br>
<div class="gmail_quote">On Sat, Jan 9, 2016 at 12:54 AM -0800, "Andrew Bennett via swift-evolution" <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="3D"ltr"">
<div dir="ltr"><div>I'd like to discuss adding a @pure keyword, and see what it requires and how possible it is to include.</div><div><br></div>I'd like to use the annotation @pure on functions, methods and closures.<div><br></div><div>This will allow us to make more guarantees about what a protocol does, and what it cannot do. It will also allow APIs like `.map` and `.forEach` to have a meaningful distinction. It could also allow for something like an assert to be removed as an optimisation with no side-effects in a release build.</div><div><div><br></div><div>If something is pure it can be annotated with @pure, if it is not-pure this will be a compile-time error. The compiler could automatically add this annotation in the interface.</div><div><br></div><div>A function, method or closure is pure if:</div><div> * all functions, methods and closures it calls are also pure (this includes referencing self from a method or property)</div><div> * it only externally references let variables of pure type, let properties on those variables, and methods/computer-properties marked as pure.</div><div><br></div>A computed property can be marked as pure like this:<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>@pure var test: A</div></blockquote><div><br>A function/method/closure signature is marked as pure like this:<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">(a: A, b: A -> B) @pure -> B</font></div></blockquote><div><br></div><div>If the function/method/closure is only pure if an argument is pure (similar to @rethrows), then you could do this:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-family:monospace,monospace">(start: A, a: A -> B) @pure(b) -> B</span></blockquote><div><span style="font-family:monospace,monospace"><br></span></div><div><font face="arial, helvetica, sans-serif">Potentially this could be extended to allow a pure closure to be composed of other pure closures:</font></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span style="font-family:monospace,monospace">func compose(a: A -> B, b: B -> C) @pure -> A </span><span style="font-family:monospace,monospace">@pure(a,b) </span><span style="font-family:monospace,monospace">-> C {</span></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span style="font-family:monospace,monospace"> return { b(a($0)) }</span></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span style="font-family:monospace,monospace">}</span></div></blockquote><div><div><div><br></div></div></div><div>Of course this doesn't preclude you from requiring some of those closures to be pure:</div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-family:monospace,monospace">func compose(a: A @pure -> B, b: B -> C) @pure -> A </span><span style="font-family:monospace,monospace">@pure(b) </span><span style="font-family:monospace,monospace">-> C {</span></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-family:monospace,monospace"> return { b(a($0)) }</span></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-family:monospace,monospace">}</span></blockquote></div><div><br></div><div><div>Impact on existing code:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">No negative impact as if automatic annotation was allowed it would only happen where it was safe to do so. If it was not allowed then libraries could be slowly annotated to be marked as pure.<br><br>c and objective-c would not be marked as pure.<br><br>Pure functions can be safely removed by the optimiser if their result is not used. From this perspective assert and print should not be marked as pure (they would have to be an exception anyway).<br><br>Ideally existing libraries would be annotated, the more the better, but this can be a gradual process.<br><br><br></blockquote></div><div><br></div></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=bRwbJtRAtyuKyjhktKFujcXtJMJ0BKcAmTwXAUGP5GFv0-2Bf-2Fosb6Z13gJyBnD5uNLSWpO74dVBUz5kAhoObI7PHTarQ-2FZIaFyxTgXFcBeV7IJXdxxmboAhACftaV-2B-2BZy2Dtk3H8gey-2B88DyLCfexKgVTUBtzBPYh3Rj-2FQ62eskXGPKkR7YY-2B8whhAyf6RnrN6boipaYv0R6UZ89vXdZz8ua0itbq2CY4Cw0U3rSMXn4-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;">
</div>
</blockquote>
</div>
</body></html>