Compile-time calculation: Difference between revisions

added Julia example
m (Added Erlang)
(added Julia example)
Line 377:
<lang J> pf10 ''
3628800</lang>
 
=={{header|Julia}}==
Julia includes a powerful macro feature that can perform arbitrary code transformations at compile-time (or technically at parse-time), and can also execute arbitrary Julia code. For example, the following macro computes the factorial of <code>n</code> (a literal constant) and returns the value (e.g. to inline it in the resulting source code)
<lang julia>macro fact(n)
factorial(n)
end</lang>
If we now use this in a function, e.g.
<lang julia>foo() = @fact 10</lang>
then the value of 10! = 3628800 is computed at parse-time and is inlined in the compiled function <code>foo</code>, as can be verified by inspecting the assembly code via the built-in function <code>code_native(foo, ())</code>.
 
=={{header|m4}}==
Anonymous user