Skip to content

Private members prevent extension search/selection #10311

Closed
@mpilquist

Description

@mpilquist

Minimized code

  scala -version
Starting scala3 REPL...
Dotty compiler version 3.0.0-M1 -- Copyright 2002-2020, LAMP/EPFL
scala> object Module {
     |   class MyInt(private val x: Int)
     |   object MyInt {
     |     implicit class Ops(self: MyInt) extends AnyVal {
     |       def x: Int = self.x
     |     }
     |   }
     | }
// defined object Module

scala> import Module._

scala> val a = new MyInt(42)
val a: Module.MyInt = Module$MyInt@165aa43a

scala> a.x
1 |a.x
  |^^^
  |value x cannot be accessed as a member of (a : Module.MyInt) from module class rs$line$4$.

scala> MyInt.Ops(a).x
val res0: Int = 42

Expectation

Because MyInt#x is private, a.x should resolve to the extension method defined by MyInt.Ops. That's how Scala 2 works. We used this pattern in scodec-bits to evolve the library without breaking binary compatibility -- in particular, scodec/scodec-bits#107 (comment).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions