Skip to content

Private members prevent extension search/selection #10311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mpilquist opened this issue Nov 13, 2020 · 0 comments
Closed

Private members prevent extension search/selection #10311

mpilquist opened this issue Nov 13, 2020 · 0 comments
Assignees

Comments

@mpilquist
Copy link
Contributor

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).

@odersky odersky self-assigned this Nov 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants