<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div>On Dec 17, 2016, at 11:15, Anton Zhilin <<a href="mailto:antonyzhilin@gmail.com">antonyzhilin@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">I was in the process of writing something along those lines :)</p>
<p style="margin:0px 0px 1.2em!important">But I would prefer this attribute to be added implicitly by the compiler.<br>Then, if we want to <em>validate</em> that some function is statically computable, we add <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">@pure</code> to the function.<br>If we want to <em>require</em> that some variable is computed at compilation time (even if it takes a lot of time, even in debug builds), we add <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">@pure</code> to the declaration.</p>
<p style="margin:0px 0px 1.2em!important">2016-12-17 21:59 GMT+03:00 David Sweeris via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>:</p>
<p style="margin:0px 0px 1.2em!important"></p><div class="markdown-here-exclude"><p></p><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It wouldn't have to be restricted to a subset of the language; just to functions whose evaluation doesn't depend on anything that happens at runtime.<br>
<br>
Any "@pure" (or whatever... it's easier to type on my phone than "@constantExpression") function should work fine, if we define a "pure" function to be something like:<br>
1) Doesn't mutate anything outside its own scope<br>
2) Doesn't call anything involving rand() or other non-deterministic functions<br>
3) Doesn't have a result that depends on the host or target architectures (I'm not sure if this extends to FP subtleties)<br>
4) Doesn't reference any non-local variables which don't themselves have a value that's either itself a literal or the result of evaluating a "pure" function<br>
5) Doesn't call any other functions which aren't themselves "pure", or instantiate any variables using inits that aren't "pure"<br>
<br>
Since there's already a Swift REPL, at least conceptually speaking, this doesn't seem too hard (although it does raise the bar a bit for what it takes to have a "full" Swift compiler, since it'd then depend on having a working REPL on the host platform, which IIRC wasn't the case on Linux for a while... dunno, maybe this is a non-issue).</blockquote></div></div></div></div></blockquote><div><br></div><div>Yeah, it seems like it'd be fairly straightforward to infer a function's "pureness" for value-type arguments and variables by annotating functions like rand() and such as "@hassideeffects" (which is a very poor wording, given that we don't want the @ stuff to be camelCase). It'd be harder for reference types, since their mutating functions aren't marked as such.</div><div><br></div><div>We could probably get away with marking known-"bad" functions as "@whatever", use "@pure" for functions which *need* to be used that way, and let the compiler try to figure out the validity of using other, unannotated functions in a constant expression.</div><div><br></div><div>Yes, I'm aware this could impact compile times, but we're just talking about assigning literals here... it doesn't need to be done for every line of code, and the functions in question aren't likely to be too complex. Presumably the results would get cached along with the other incremental compilation data (I think that's the right term), as well.</div><div><br></div><div>Regarding terminology, there's a subtle difference between my list of constraints and what I'd normally associate with "pure" in the functional programming sense of the word... Specifically, in the FP meaning, I'd change #4 to "doesn't access any non-local variables" (which would make #1 redundant). Because of that, if this goes to a formal proposal, I prefer to use something else. "@constexpr" works for me, but I don't actually have much of an opinion on the topic, other than "@pure", IMHO, should loosely refer to code that's safely auto-parallelizable, reentrant, memoizable, etc (which is all a whole 'nother topic, but I wanted to reserve the word, so to speak).</div><div><br></div><div>- Dave Sweeris </div></body></html>