scala - how to call a subclass constructor with constrained access? -
i have below want compile
trait t{ def a: int } class private(val a: int, a2: int) extends t{ def add(b: int): = new a(a + b, a2 + 2*b) } object t{ def make(a: int): = { new a(a, 2*a) // doesn't compile }
here class has private constructor because want way construct a pass through constructor t.make
or updated method a.add
(which in example both garantie a2 = 2*a).
but because constructor private, cannot access parent trait t. same goes protected. basically, need inversed protected?
note: solution not satisfying lower (too much) restriction on constructor, maybe package-private. don't want other construction possible ones planned. lower constraint code in current file, don't know how either.
a few options.
you can rename
object t
object a
- have access a's private membersyou can make
class a
innerobject t
- can make constructorprivate[t]
, ,t
have access it.you can make package-private. mistaken when "lowering restriction much". package in scala doesn't have entire directory. doesn't need whole file. have complete control on content, , purpose of able trying here.
Comments
Post a Comment