我可以在不透明类型的扩展中使用Scala 3导出来选择性地公开内部类型方法吗?

7d7tgy0s  于 2023-11-18  发布在  Scala
关注(0)|答案(2)|浏览(148)

我希望在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 as BigNumber

字符串
这样做会很方便,但它不起作用:/
什么是巡视??

nfs0ujit

nfs0ujit1#

Michał Pałka向我展示了这个工作黑客:

object OpaqueScope:
  opaque type BigNumber = BigInt

  object BigNumber:
    def apply(s: String): BigNumber = BigInt(s)

    extension (cv: BigNumber)
      def value: BigInt = cv
      export value.{unary_- as myUnary}

import OpaqueScope.BigNumber
val c = BigNumber("7")

c.myUnary // returns -7 as BigInt

字符串
官方文件里也有。

DRAWBACK:类型不保留:(

bvjxkvbb

bvjxkvbb2#

为了保留返回类型BigNumber,你不能像2023一样使用export

object OpaqueScope:
  opaque type BigNumber = BigInt

  object BigNumber:
    def apply(s: String): BigNumber = BigInt(s)

    extension (cv: BigNumber)
      def myUnary: BigNumber = cv.unary_-

import OpaqueScope.BigNumber
val c = BigNumber("7")

c.myUnary // returns -7 as BigNumber

字符串

相关问题