我希望在 Scala 3 中使用不透明类型进行导出:
opaque type BigNumber = BigInt
object BigNumber {
def apply(s: String): BigNumber = BigInt(s)
extension (cv: BigNumber)
export cv.(unary_- as myUnary}
}
val c = BigNumber("7")
c.myUnary // I wish to have -7
这样工作会非常方便。 但它不起作用:/
什么是解决方法?
PS:我使用了不同的别名来更轻松地进行复制/粘贴测试,因为名称未更改,您需要退出该不透明类型定义的范围才能看到它通常不可用。
Michał Pałka 向我展示了这个可行的技巧:
opaque type BigNumber = BigInt
object BigNumber {
def apply(s: String): BigNumber = BigInt(s)
extension (cv: BigNumber)
private def cv0 = cv
export cv0.{unary_- as myUnary}
}
val c = BigNumber("7")
import BigNumber.myUnary
c.myUnary // returns -7
甚至在官方文档中也有。
我希望不需要 hack。