Last active
January 25, 2022 14:00
-
-
Save deanwampler/31914a7c6165e4a3a0ce6fd05dbba255 to your computer and use it in GitHub Desktop.
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.ops.int.* | |
// Only allowed values are Min <= N <= Max. | |
type Bounded[MIN <: Int, MAX <: Int] <: Int = MAX match | |
case MIN => MIN | |
case ? => MAX | Bounded[MIN,MAX-1] | |
val zero15: Bounded[1,5] = 0 // ERROR | |
val one15: Bounded[1,5] = 1 | |
val two15: Bounded[1,5] = 2 | |
val three15: Bounded[1,5] = 3 | |
val four15: Bounded[1,5] = 4 | |
val five15: Bounded[1,5] = 5 | |
val six15: Bounded[1,5] = 6 // ERROR | |
// A type for allowed indices, zero-based, for an indexed collection of size N. | |
type IndexOf[N <: Int] = Bounded[0,N-1] | |
val zero5: IndexOf[5] = 0 | |
val one5: IndexOf[5] = 1 | |
val two5: IndexOf[5] = 2 | |
val three5: IndexOf[5] = 3 | |
val four5: IndexOf[5] = 4 | |
val five5: IndexOf[5] = 5 // ERROR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment