<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">My vision of “pure” functions was the following:</p>
<ol style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px">Compiler automatically marks all functions and expressions as pure, wherever possible<ul style="margin:1.2em 0px;padding-left:2em;margin:0px;padding-left:1em">
<li style="margin:0.5em 0px">We should be interested not in “Haskell-ish pure” functions, but in “computable during compilation” functions</li>
<li style="margin:0.5em 0px">Therefore I prefer to use <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">@constexpr</code> or <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">const</code> instead of <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></li>
</ul>
</li>
<li style="margin:0.5em 0px">We can mark a function as <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">const</code> to <em>assert</em> that it is indeed pure</li>
<li style="margin:0.5em 0px">We can mark a variable as <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">const</code> to <em>ensure</em> that it’s computed at compilation time<ul style="margin:1.2em 0px;padding-left:2em;margin:0px;padding-left:1em">
<li style="margin:0.5em 0px">Compiler might compute some non-<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">const</code> expressions, but no guarantees given</li>
</ul>
</li>
</ol>
<p style="margin:0px 0px 1.2em!important">One issue is, we don’t have or suggest any facilities to make use of pure functions, other than some optimization, which can be performed anyway as of now.</p>
<p style="margin:0px 0px 1.2em!important">One use-case would be conversion of metatypes to types:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><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;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important">const let x: Any = makeSomething()
typealias T = type(of: x)
</code></pre><p style="margin:0px 0px 1.2em!important">This feature can be powerful enough to fill the niche of macros in Swift, without unsafety of C++ or specific syntax of Rust.</p>
<p style="margin:0px 0px 1.2em!important">2017-02-17 14:14 GMT+03:00 Haravikk 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"><div style="word-wrap:break-word"><div>I like the idea of having pure functions in Swift, but my first thought is; should we have to declare it at all? Is it not easier to just have the compiler automatically flag a function as pure or not?</div><div><br></div><div>With that in mind we don't need any new syntax, but a simple @pure attribute should be sufficient. This can be used anywhere that a function is declared, or a closure is accepted as a parameter, allowing us to be explicit that we are trying to define a pure function, or only accept pure closures.</div><div><br></div><div>The big benefit of this is that it is retroactive; all existing functions that are pure will be automatically detected as such, and can be passed into any method accepting only pure functions. The new capability will be that developers can specify that a function *must* be pure and thus produce an error if it isn't.</div></div></blockquote><p></p></div><p style="margin:0px 0px 1.2em!important"></p>
<div title="MDH:TXkgdmlzaW9uIG9mICJwdXJlIiBmdW5jdGlvbnMgd2FzIHRoZSBmb2xsb3dpbmc6PGRpdj48YnI+
PC9kaXY+PGRpdj4xLiBDb21waWxlciBhdXRvbWF0aWNhbGx5IG1hcmtzIGFsbCBmdW5jdGlvbnMg
YW5kIGV4cHJlc3Npb25zIGFzIHB1cmUsIHdoZXJldmVyIHBvc3NpYmxlPC9kaXY+PGRpdj4mbmJz
cDsgKiBXZSBzaG91bGQgYmUgaW50ZXJlc3RlZCBub3QgaW4gIkhhc2tlbGwtaXNoIHB1cmUiIGZ1
bmN0aW9ucywgYnV0IGluICJjb21wdXRhYmxlIGR1cmluZyBjb21waWxhdGlvbiIgZnVuY3Rpb25z
PC9kaXY+PGRpdj4mbmJzcDsgKiBUaGVyZWZvcmUgSSBwcmVmZXIgdG8gdXNlIGBAY29uc3RleHBy
YCBvciBgY29uc3RgIGluc3RlYWQgb2YgYEBwdXJlYDwvZGl2PjxkaXY+Mi4gV2UgY2FuIG1hcmsg
YSBmdW5jdGlvbiBhcyBgY29uc3RgIHRvICphc3NlcnQqIHRoYXQgaXQgaXMgaW5kZWVkIHB1cmU8
L2Rpdj48ZGl2PjxkaXYgY2xhc3M9ImdtYWlsX2V4dHJhIj4zLiBXZSBjYW4gbWFyayBhIHZhcmlh
YmxlIGFzIGBjb25zdGAgdG8gKmVuc3VyZSogdGhhdCBpdCdzIGNvbXB1dGVkIGF0IGNvbXBpbGF0
aW9uIHRpbWU8L2Rpdj48ZGl2IGNsYXNzPSJnbWFpbF9leHRyYSI+Jm5ic3A7ICogQ29tcGlsZXIg
bWlnaHQgY29tcHV0ZSBzb21lIG5vbi1gY29uc3RgIGV4cHJlc3Npb25zLCBidXQgbm8gZ3VhcmFu
dGVlcyBnaXZlbjwvZGl2PjxkaXYgY2xhc3M9ImdtYWlsX2V4dHJhIj48YnI+PC9kaXY+P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