<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I would rather you mark a function as impure and have the default as pure!<div class=""><br class=""><br class=""><br class="">
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On 9 Jan 2016, at 7:53 PM, Andrew Bennett via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">I'd like to discuss adding a @pure keyword, and see what it requires and how possible it is to include.</div><div class=""><br class=""></div>I'd like to use the annotation @pure on functions, methods and closures.<div class=""><br class=""></div><div class="">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 class=""><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">A function, method or closure is pure if:</div><div class=""> * all functions, methods and closures it calls are also pure (this includes referencing self from a method or property)</div><div class=""> * it only externally references let variables of pure type, let properties on those variables, and methods/computer-properties marked as pure.</div><div class=""><br class=""></div>A computed property can be marked as pure like this:<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class="">@pure var test: A</div></blockquote><div class=""><br class="">A function/method/closure signature is marked as pure like this:<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="monospace, monospace" class="">(a: A, b: A -> B) @pure -> B</font></div></blockquote><div class=""><br class=""></div><div class="">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" class=""></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><span style="font-family:monospace,monospace" class="">(start: A, a: A -> B) @pure(b) -> B</span></blockquote><div class=""><span style="font-family:monospace,monospace" class=""><br class=""></span></div><div class=""><font face="arial, helvetica, sans-serif" class="">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" class=""><div class=""><span style="font-family:monospace,monospace" class="">func compose(a: A -> B, b: B -> C) @pure -> A </span><span style="font-family:monospace,monospace" class="">@pure(a,b) </span><span style="font-family:monospace,monospace" class="">-> C {</span></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><span style="font-family:monospace,monospace" class=""> return { b(a($0)) }</span></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><span style="font-family:monospace,monospace" class="">}</span></div></blockquote><div class=""><div class=""><div class=""><br class=""></div></div></div><div class="">Of course this doesn't preclude you from requiring some of those closures to be pure:</div><div class=""><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><span style="font-family:monospace,monospace" class="">func compose(a: A @pure -> B, b: B -> C) @pure -> A </span><span style="font-family:monospace,monospace" class="">@pure(b) </span><span style="font-family:monospace,monospace" class="">-> C {</span></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><span style="font-family:monospace,monospace" class=""> return { b(a($0)) }</span></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><span style="font-family:monospace,monospace" class="">}</span></blockquote></div><div class=""><br class=""></div><div class=""><div class="">Impact on existing code:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="">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 class=""><br class="">c and objective-c would not be marked as pure.<br class=""><br class="">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 class=""><br class="">Ideally existing libraries would be annotated, the more the better, but this can be a gradual process.<br class=""><br class=""><br class=""></blockquote></div><div class=""><br class=""></div></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Vm9j-2B2K6zLqxUFTO82XA8HV2TThDz5lA3-2F-2Fpeujw7DSrVPnP3caZaVllbVBnz-2FZs1lAXgswDGxkSarVozl8lyWwpPF5NgqEXdLRmAHjS2gijoNKbKYUO9axCjMjWNwocOJg2jPcqG7WpLnASa-2F2cre0HzP3TdnfdbaYjGyNu-2BAdejzv9cwJLF6aK78a0EoiFsQ7wRpeC0gu9o7W6uOyEZu5s9qW-2Fp2mdFuDD-2FAbATF4-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;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>