Reputation: 971
I'm currently looking how covariant and contravariant type parametrization is handled in Scala. And there's a special case, where a var field must be private[this] in order to compile. From example in this code snippet (taken in this question: private[this] vs private):
class Holder[+T] (initialValue: Option[T]) {
// without [this] it will not compile
private[this] var value = initialValue
def getValue = value
def makeEmpty { value = None }
}
I understand this example. But what I can't understand, is in what case is a private field accessible from another instance than itselt (this)?
Thanks for your help.
Upvotes: 2
Views: 508
Reputation: 36522
If a member is plain-private, it is accessible from other instances of the same class. The [this]
suffix makes it visible only to the instance that contains it.
Upvotes: 5