我想用scala类型系统和常量来表示一些受限数据。 例如,在伪代码,我想这样写是这样的,(和optionaly如果可能的话,使用编译时检查的约束上)如何使用泛型约束类型
val data1 : String of 10
val data2 : Int of (0..10)
val data3 : Int of (1..1000)
val data4 : String of 30
// etc.
无需编写这样的代码为每个类型:
type Tagged[U] = { type Tag = U }
type @@[T, U] = T with Tagged[U]
object Tag {
@inline def apply[T, U](t: T): T @@ U = t.asInstanceOf[T @@ U]
}
sealed trait StringOf32
object StringOf32 {
def apply(value : String) : String @@ StringOf32 = {
require(value.length <= 32)
Tag(value)
}
def unapply(s : String @@ StringOf32) : Option[String] = Some(s)
}
sealed trait IntFrom0To10
object IntFrom0To10 {
def apply(value : Int) : Int @@ IntFrom0To10 = {
require(value >= 0 && value <= 10)
Tag(value)
}
def unapply(s : Int @@ IntFrom0To10) : Option[Int] = Some(s)
}
// etc.
是否有alrealdy存在一些图书馆这样的建设? 有没有办法,有这样的通用构造? 也许使用宏,但我不确定这是一个好主意,而且我也不流利。
你觉得我应该往哪个方向走?
是的,它看起来像我想要的。谢谢你的指针 – volia17