<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Comment in-line below.<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 9 Jan 2016, at 8:04 PM, Andrew Bennett <<a href="mailto:cacoyi@gmail.com" class="">cacoyi@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">That would be nice, although I'm not sure how practical that is in current swift usage. I imagine it would require a lot of changes to existing code and libraries.</div></div></blockquote><div><br class=""></div>Not sure how hard this would be or if a migration tool and enhanced Obj-C importer would be good enough.</div><div><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="">Is a method impure if it uses self? I suppose it could be. I guess self is an inout parameter. I presume an inout parameter is a known expected side-effect.</div></div></blockquote><div><br class=""></div><div>If it doesn’t change self then it is pure.</div><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="">Closures would probably be the largest impact if things were pure by default.</div></div></blockquote><div><br class=""></div>I think this would be a great change. We are part way there with @noescape and @autoclosure already</div><div><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="">It would be interesting though to do a code survey and see what portion would need to be marked as @impure, and what conditions lead them to that requirement.</div><div class=""><br class=""></div><div class="">I'm happy to discuss @impure too, but my focus is still @pure, as I think it is a more attainable target, at the moment.</div><div class=""><br class=""></div><div class="">Perhaps after everything is annotated, impure functions are minimised, and if swift is streamlined for pure function use, then we can introduce @impure and deprecate @pure.</div></div></blockquote><div><br class=""></div>Not sure either will happen any time soon :(. Therefore the most useful thing would be the survey you suggest.</div><div><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="">On Sat, Jan 9, 2016 at 7:56 PM, Howard Lovatt <span dir="ltr" class=""><<a href="mailto:howard.lovatt@gmail.com" target="_blank" class="">howard.lovatt@gmail.com</a>></span> wrote:</div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" 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 class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On 9 Jan 2016, at 7:53 PM, Andrew Bennett via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class=""></div></div><div class=""><div class=""><div class="h5"><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>
</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="min-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" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></blockquote></div><br class=""></div>
</blockquote></div><br class=""></div></body></html>