Skip to content
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

Private constructor pollute visibility of methods #6671

Closed
andreaTP opened this issue Jun 12, 2019 · 3 comments
Closed

Private constructor pollute visibility of methods #6671

andreaTP opened this issue Jun 12, 2019 · 3 comments

Comments

@andreaTP
Copy link
Contributor

Using a private constructor with case classes cause other methods to be considered private.

Repro 1:

case class X3 private (foo: Int)
val x = X3(1)

trigger:

constructor X3 cannot be accessed as a member of X3

Repro 2:

object X2 {
  def apply(
    foo: Int,
    bar: String = ""
  ) = new X2(foo, bar)
}

case class X2 private (
  foo: Int,
  bar: String = ""
)

val x = X2(1)
x.copy(1, "2")

trigger:

method copy in class X2 cannot be accessed as a member of X2

Both examples compiles with scalac 2.12 because only the canonical constructor is affected by the restriction of visibility.
i.e. both the apply in the companion object and the copy method should be accessible.

@joroKr21
Copy link
Member

That's a feature!

@smarter
Copy link
Member

smarter commented Jun 19, 2019

Yep, it's a feature: #5908. It hasn't been merged in Scala 2 yet but it's coming hopefully: scala/scala#7702

@smarter smarter closed this as completed Jun 19, 2019
@smarter
Copy link
Member

smarter commented Jun 19, 2019

Note that you should be able to hand-write your own apply or copy methods with the same signature as the default generated ones, and the compiler will just not generate these methods itself, so you can define them with whatever accessibility you want.

giabao added a commit to ohze/akka that referenced this issue Mar 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants