In scala I can add access modifiers to a class/trait/object, like
private class Foo
protected[this] trait Foo
I have not found any good explanation on these class/trait/object-level modifiers. Do all such combinations make sense and what do they actually mean?
They mean the same as access modifiers for class/trait members, as classes and traits can also be members of other classes. For example:
class A {
private class Foo
}
Class Foo is only visible to class A. If I change the modifier to private[this], then it is called object private, and so any Foo is only visible to it's parent instance of A.
Declaring private, private[this], protected or protected[this] only really makes sense within another class or trait, because it has to be private to something. In this case, Foo being private to A. The same applies to traits.
We could also not have a containing object, and make them package private.
package com.example.foo
private[foo] class Foo
Now class Foo is only visible to other members of the package com.example.foo.
Do they make sense? In some cases I'm sure it is useful to have private classes and traits within some other object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With