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?

  • 2
    \$\begingroup\$ I'm not going to post a full answer right now because I'd like to think more. But I think it would probably be all around best if you just made a harness for running these types of answers. This would technically make them a new language, and would make all existing rules clearly apply to these types of answers. \$\endgroup\$ – Wheat Wizard Dec 25 '20 at 22:34
  • 1
    \$\begingroup\$ You can also do this stuff in Haskell, and it has been done \$\endgroup\$ – Wheat Wizard Dec 25 '20 at 22:36
  • \$\begingroup\$ @WheatWizard That makes sense. For my own answers, I did write tests, but if the only way of checking a correct result is doing 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
  • 1
    \$\begingroup\$ Sorry I think I was unclear, those two comments are unrelated to each other. I was suggesting that you write a program that takes in a C++ (or Scala or Haskell) program written in this fashion and attaches a bit of boilerplate code to the end to turn it into a complete program. This program is technically a compiler so what you have is a "new" programming language, you can call it C++ templates or something. Since it is a complete and standardized programming language there is no need for any special rules or meta rulings. \$\endgroup\$ – Wheat Wizard Dec 25 '20 at 22:58
  • \$\begingroup\$ @WheatWizard Oh, I see. I guess that solves my issue. \$\endgroup\$ – user Dec 25 '20 at 22:59

You must log in to answer this question.

Browse other questions tagged .