Anonymous user
Compile-time calculation: Difference between revisions
→{{header|Scala}}: Add Scala 3 version with proper CTE
m (Reformatting a long piece of text.) |
imported>Bkranjc (→{{header|Scala}}: Add Scala 3 version with proper CTE) |
||
Line 1,459:
=={{header|Scala}}==
Scala 3 supports proper compile time evaluation
<syntaxhighlight lang="scala">
transparent inline def factorial(inline n: Int): Int =
inline n match
case 0 => 1
case _ => n * factorial(n - 1)
inline val factorial10/*: 3628800*/ = factorial(10)
</syntaxhighlight>
Alternative version that works with Scala 2:
<syntaxhighlight lang="scala">object Main extends {
val tenFactorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
|