Skip to content

Instantly share code, notes, and snippets.

@tarao
Created March 9, 2022 02:56
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 tarao/853248d8be6fad527693c0d58e0353d6 to your computer and use it in GitHub Desktop.
Save tarao/853248d8be6fad527693c0d58e0353d6 to your computer and use it in GitHub Desktop.
import scala.compiletime.ops.{any, int}
trait Odd
object Odd {
type IsOdd[I <: Int] = any.!=[int.%[I, 2], 0]
def apply[I <: Int](i: I)(using IsOdd[I] =:= true): Int & Odd =
i.asInstanceOf[Int & Odd]
}
trait Even
object Even {
type IsEven[I <: Int] = any.==[int.%[I, 2], 0]
def apply[I <: Int](i: I)(using IsEven[I] =:= true): Int & Even =
i.asInstanceOf[Int & Even]
}
val i1 = Odd[1]
// val i2 = Odd[2] // compile error
// val i3 = Even[3] // compile error
val i4 = Even[4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment