<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On 8 Feb 2016, at 04:29, Steve Richey via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; font-size: 14px; font-family: Calibri, sans-serif;" class=""><div class=""><br class="">
</div>
<div class="">Example:</div>
<div class="">```</div>
<div class="">func myFunction() -> String {</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>return "hello"</div>
<div class="">}</div>
<div class=""><br class="">
</div>
<div class="">let myValue = #run myFunction()</div>
<div class="">```</div>
<div class=""><br class="">
</div>
<div class="">At compile time, `myFunction` is evaluated and the result inlined to the `myValue` definition. At run time, `myValue` is a `String` containing `"hello"`.</div>
<div class=""><br class="">
</div>
<div class="">This is useful for tasks that are relatively expensive to run but only need to be done once, such as lookup tables. Running the algorithm to generate those tables can be handled at compile-time, and the results retrieved at no cost at run time. Furthermore,
this structure allows code reuse between the run time and build time code, obviating the need to perform similar tasks in, say, a Swift method and a Python script.</div></div></div></blockquote><br class=""></div><div>Do we actually need the #run attribute in this case? If the compiler can detect that myFunction() has a fixed return value, or is only ever called once etc., then could it not just optimise away the function call entirely?</div><div><br class=""></div><div>For example:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func powersOfTwo() -> [UIntMax] { // Return an array of the first 64 powers of two.</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>var steps = (sizeof(UIntMax) * 8)</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>var powers[UIntMax] = [];</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>repeat {</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>steps -= 1</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>powers.append(UIntMax(1) << steps)</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>} while (steps > 0)</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return powers</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><font face="Monaco" class=""><br class=""></font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let powersOfTwoList = powersOfTwo()</font></div><div><br class=""></div><div>Since powersOfTwo() is only ever used once, it can be computed into an array constant; the compiler might already do that, I’m not sure. The main difficulty is whether the compiler can detect code that has no predictable return value, e.g- a call that returns the current time, as this would prevent a function from being precomputed into a constant in this way. There may also be cases where a function’s precomputed value could be very large in which case it might be more desirable to compute it only as needed, though if powersOfTwoList were a static value this wouldn’t matter.</div><div><br class=""></div><div>In other words, I think we should be clear on where macros will actually offer functionality that the compiler can’t provide for us, as it seems to me that in the examples given so far we could just use regular code and the compiler can factor it out for us, or we could have an attribute that allows us to indicate which code the compiler should try to factor out. But as a general rule I think if the compiler can factor out unnecessary functions itself, then this could be better in the long run for efficiency anyway, and that’s assuming it doesn’t already do this to some degree (hopefully someone can weigh in on that).</div></body></html>