Skip to content

Instantly share code, notes, and snippets.

@jooohn
Created July 20, 2019 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jooohn/e0bfcfeeeed6a33369f7a12d20e522ad to your computer and use it in GitHub Desktop.
Save jooohn/e0bfcfeeeed6a33369f7a12d20e522ad to your computer and use it in GitHub Desktop.
Compile time FizzBuzz in Dotty
import scala.compiletime._
object Main {
type -[A <: Int, B <: Int] <: Int = B match {
case 0 => A
case S[b] => A match {
case S[a] => a - b
case _ => Nothing
}
}
type %[A <: Int, B <: Int] <: Int = A - B match {
case Nothing => A
case _ => (A - B) % B
}
type FizzBuzzValue = "Fizz" | "Buzz" | "FizzBuzz" | Int
type FizzBuzzValueFor[N <: Int] <: FizzBuzzValue =
(N % 3, N % 5) match {
case (0, 0) => "FizzBuzz"
case (0, _) => "Fizz"
case (_, 0) => "Buzz"
case _ => N
}
type ~[From <: Int, To <: Int] <: Tuple =
From match {
case To => From *: Unit
case _ => From *: S[From] ~ To
}
inline def printConstValues[T]: Unit = inline erasedValue[T] match {
case _: Unit => ()
case _: (h *: t) =>
println(constValue[h])
printConstValues[t]
}
def main(args: Array[String]): Unit = {
printConstValues[Tuple.Map[1 ~ 30, FizzBuzzValueFor]]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment