Created
January 4, 2025 04:22
-
-
Save mbbx6spp/5cd08a270fd96bd7dad9f3beb4bd941d to your computer and use it in GitHub Desktop.
Yesterday Jan 2, I had fun with the compiler and a coworker with the following inlines. :)
This file contains hidden or 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
inline def CURRENT_YEAR = 2025 | |
opaque type CurrentOrFutureYear <: Int = Int | |
object CurrentOrFutureYear: | |
inline def apply(n: Int) = | |
inline if (n >= CURRENT_YEAR) n: CurrentOrFutureYear | |
else compiletime.error("Given year is in the past") | |
inline def make(inline n: Int): Option[CurrentOrFutureYear] = | |
Option.when(n > CURRENT_YEAR)(n) | |
extension (inline n: Int) | |
inline def asCurrentOrFutureYear = CurrentOrFutureYear(n) | |
val thisYear = 2025.asCurrentOrFutureYear | |
// Uncomment line below to see compile-time error with custom message (see above) | |
//val lastYear = 2024.asCurrentOrFutureYear | |
@main def run = println( | |
s"Happy New Year (${CURRENT_YEAR}). May your inlines compile as quick as you quit new year resolutions." | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess it's really time for me to learn what's new in Scala 3...