I want to hide constructor in one class along with its fields and only create instances using a companion object, but I can't achieve that. I have scala 2.13.3 and it's based on java 8. Here is a code sample:
A.scala
package X
object A {
def apply(x: Int): A = A(Seq(x))
}
case class A private(private val s: Seq[Int])
B.scala
package Y
import X.A
class B {
val b = A(Seq.empty)
}
And although I wanted to make only apply(x:Int)
visible this code compiles so the private constructor is also visible. How can I change this code to work as I expected?
Make the method
A.apply(Seq[Int])
private tooA.scala
B.scala
Here the line
val b = A(Seq.empty)
produces errori.e. the method
A.apply(Seq[Int])
is not visible fromB
.