<div dir="ltr">Dmitri,<div><br></div><div><div>Thanks, that's a good point. That's related to why I was wondering about self as inout in my reply to Howard. My rules were a starting point, I'm sure there's things that can be improved. I'm glad we've got the conversation started :)</div><div><br></div><div>The type of <font face="monospace, monospace">Array<Int>.sort</font> is <font face="monospace, monospace">(Array<Int>)</font><span style="font-family:monospace,monospace"> -> () -> </span><span style="font-family:monospace,monospace">Array<Int></span></div></div><div><span style="font-family:monospace,monospace"><br></span></div>This type could be marked as pure like this:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">(Array<Int>) @pure</font><span style="font-family:monospace,monospace"> -> () </span><span style="font-family:monospace,monospace">@pure </span><span style="font-family:monospace,monospace">-> </span><span style="font-family:monospace,monospace">Array<Int></span></div><div><span style="font-family:monospace,monospace"><br></span></div></blockquote>The partial application of the function captures the current state of the input array (value type), then the full application of the function sorts and returns that array. I don't think there's a problem with purity here, there's on *external* references.<div><br></div><div>It gets more interesting with a mutating function, <span style="font-family:monospace,monospace">Array<Int>.</span><font face="monospace, monospace">sortInPlace</font> is potentially pure as well:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">(inout Array<Int>) @pure</font><span style="font-family:monospace,monospace"> -> Void</span></div></blockquote><div style="font-family:monospace,monospace"><font face="monospace, monospace"><br></font></div>This type signature could be considered equivalent to the type of <span style="font-family:monospace,monospace">Array<Int>.sort</span>. You're taking an array in, and sending an array out. It's replacing the in array with the out one.<div><br></div><div>I think the important thing is that everything affected by the function is in the type signature.</div><div><br></div><div>Andrew</div><div><div><span style="font-family:monospace,monospace"><br></span></div><div><span style="font-family:monospace,monospace"><br></span></div><div><span style="font-family:monospace,monospace"><br></span></div><div><div><div><div><div><br></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 9, 2016 at 9:29 PM, Dmitri Gribenko <span dir="ltr"><<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="">On Sat, Jan 9, 2016 at 10:53 AM, 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><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>A function, method or closure is pure if:<br></div><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></div></blockquote><div><br></div></span><div>I'm concerned that with this definition we won't be able to mark many APIs as pure, even though they actually are pure. The issue is that this definition disallows local mutation. Consider CollectionType.sort() -- the way it is implemented is that it first copies the collection into an array, and then sorts that array in-place. sortInPlace() isn't pure, but because the mutation happens on local state, the whole operation is pure.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Dmitri</div></font></span></div><span class="HOEnZb"><font color="#888888"><div><br></div>-- <br><div>main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>>*/</div>
</font></span></div></div>
</blockquote></div><br></div>