Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Created January 4, 2025 04:22
Show Gist options
  • Save mbbx6spp/5cd08a270fd96bd7dad9f3beb4bd941d to your computer and use it in GitHub Desktop.
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. :)
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."
)
@toby5box
Copy link

I guess it's really time for me to learn what's new in Scala 3...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment