Can I use Scala 3 export in extension of opaque type to expose the inner type methods selectively?

55 Views Asked by At

I would like to have exports working with opaque tyeps in 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 

It would be very handy to work that way. But it doesn't work :/

What is the walkaround?

2

There are 2 best solutions below

0
Waldemar Wosiński On

Michał Pałka has shown me this working hack:

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

It's even in the official documentation.

DRAWBACK: type is not preserved :(

0
Waldemar Wosiński On

To preserve the return type BigNumber you can't use export as for 2023:

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