I have an abstract class in the package foo (in this particular case, a sign) that can be implemented by various subclasses, and I would like to create an orthogonal subclass for use in a more specific package bar that adds information specific to the package. It seems that the best way is wrapping, not inheritance, because otherwise I would have to declare specific package versions for each of the subclasses of the foo package. But this leads to a problem with protected members that need to be redirected:
package foo { trait Foo { protected def bar: Int } } package bar { import foo.Foo class Baz class WrapFoo(wrapped: Foo) extends Baz with Foo { protected def bar = wrapped.bar } }
This results in an error:
~/test/scala 14:54 152272% scalac testprotected.scala testprotected.scala:11: error: method bar in trait Foo cannot be accessed in foo.Foo Access to protected method bar not permitted because prefix type foo.Foo does not conform to class WrapFoo in package bar where the access take place protected def bar = wrapped.bar ^ one error found
Even if WrapFoo is a subclass of foo , scala does not like calling wrapped.bar . I assume this is because an object of type WrapFoo not a wrapped sub-object.
Question: what idiomatic way to declare protection on bar different than just making it publicly available? The bar function is intended to be called by other functions in foo , not publicly. scala has an expressive defense system, but I donβt quite understand it. Is this even possible?
source share