Created
July 20, 2019 14:25
-
-
Save jooohn/e0bfcfeeeed6a33369f7a12d20e522ad to your computer and use it in GitHub Desktop.
Compile time FizzBuzz in Dotty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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