I have a couple answers here that work at compile-time and just posted another, and I was wondering if such solutions are allowed. I'm not talking about just proper functions that can run at compile-time, such as constexpr
in C++, but also stuff like types that take type parameters and return other types or templates that do something depending on their arguments.
For example, here is a factorial function in Scala 3 using types and some imported black magic:
import compiletime.ops.int._
type Fac[X <: Int] <: Int = X match {
case 0 => 1
case _ => X * Fac[X - 1]
}
This does not return a value that can be directly printed. Instead, you would need compiletime.constValue[Fac[6]]
to compute the factorial of 6
, and not all languages that allow compile-time shenanigans also allow you to turn a type into a value so easily. You might need to verify it using something like val check: Fac[6] = 720
and see if the compiler throws an error or not instead - it's not as simple as just print(factorial(6))
.
Are such submissions valid? And if so, what rules do they have to abide by?
val check: Fac[6] = 720
in some language, then that complicates things quite a bit. The Haskell question specifically asks for something to operate over types, but what about for "normal" questions where a program/function is expected? \$\endgroup\$ – user Dec 25 '20 at 22:42