While fighting with my private immutable class constructor, and the constraint that auxiliary constructors have to call each other as first statement, without anything else from the class in scope, I seem to be constrained to use a companion object for my instantiations, and since my companion object would have to access the main constructor, I need the private keyword to target a scope including that object.
Now, my brain is weak in name generation, and I am trying to save the need of an enclosing namespace for both that companion object and the class by placing my class within the companion object itself, this way:
object Group {
private def complexComputeX(z: Int) = z
private def complexComputeY(x: Int, z: Int) = x + z
def apply(z: Int) = {
val x = complexComputeX(z)
val y = complexComputeY(x, z)
new Group(x, y)
}
class Group private[Group](x: Int, y: Int) {
//...
}
}
val x = Group(5)
//...
The problem is that the Group
of private[Group]
does not reference the object, but still the class (making it superfluous).
How can I tag that constructor to be available at the companion object level, but not outside it?
PS: that companion object is already giving me headache, and I would even have preferred to have just the class, en-scoping there the complexCompute
, which several constructors implementations could need...
EDIT: Okay. Just while adding the tags I hit a neuron ringing me that a companion object might have some privilege over the class' scope. It can access its private parts, and so I can simply have object and class side to side without dedicated enclosing scope. However, I maintain the question, for both a response on possibility way to handle scoping for such boxing cases object Main {object Main {object Main...
and for chances of remarks about techniques for having only constructors in the class without any companion object.