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.
That's a feature!
Yep, it's a feature: https://github.com/lampepfl/dotty/pull/5908. It hasn't been merged in Scala 2 yet but it's coming hopefully: https://github.com/scala/scala/pull/7702
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.
Most helpful comment
That's a feature!