如何在 Scala 3 中实现类型级操作(在本例中为加法)?
这是我想做的(这不会编译):
case class foo[S <: Int & Singleton](value: Double) {
def bar[T <: Int & Singleton](that: foo[T]): foo[S + T] = new foo[S + T](this.value * that.value)
}
val t1 = foo[1](1.5)
val t2 = foo[1](2.0)
val t3 = t1 bar t2 //foo[2](3.0)
PS:SIP-23 提案讨论了在 Scala 2 中实现此类行为的库,但我对 Scala 3 感兴趣。
你需要导入
scala.compiletime.ops.int.+