scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

type "leakage" with transparent methods on opaque types

Open erikerlandson opened this issue 1 month ago • 5 comments

Compiler version

3.4.2-RC1

Minimized code

opaque type Repro = Int
object Repro:
    extension(v: Int)
        // lift raw Int
        inline def asRepro: Repro = v
    extension(r: Repro)
        // this doesn't need to be transparent but my actual
        // use case does
        transparent inline def *(rr: Repro): Repro =
            r * rr

object repro:
    import Repro.asRepro
    val x = 2.asRepro
    // this "leaks" the type 'Int'
    val y: (Repro & Int) = x * x
    // this should fail but doesn't
    val z = 1 + y

Output

The * method returns type Repro & Int, due to transparent keyword. In other words, it leaks the implementation type Int

Expectation

I need a way for transparent methods to return only Repro, not Repro & Int, to prevent leaky abstraction of the opaque type.

erikerlandson avatar May 18 '24 17:05 erikerlandson