I have something like below that I want to compile
trait T{
def a: Int
}
class A private(val a: Int, a2: Int) extends T{
def add(b: Int): A = new A(a + b, a2 + 2*b)
}
object T{
def make(a: Int): A = {
new A(a, 2*a) // doesn't compile
}
Here class A has a private constructor because I want the only way to construct a A is to pass through the constructor T.make
or with the updated method A.add
(which in this example both garantie that a2 = 2*a).
But because A constructor is private, it cannot be access by the parent trait T. Same goes for protected. Basically, I would need an inversed protected?
Note: A solution that is not satisfying is to lower (too much) the restriction on A constructor, maybe to package-private. But I don't want any other construction possible but the ones that were planned. I could lower the constraint to all code in the current file, but I don't know how to do that either.