Scala parameters for access modifiers?

What's the difference between

class Test {
  private[this] val foo = 0
}

      

against

class Test {
  private val foo = 0
}

      

What can everything go into []

? Also, what should I be looking for when I want to look at the specs of this? I tried various combinations of "scala access modifier arguments / parameterized scala access modifier" and nothing worked.

+3


source to share


2 answers


what should i look for when i want to look at the specs of this?

In Scala, the Language Specification is defined as an "access modifier" and an "access qualifier" (see BNF in §5.2).

What's the difference between

...



That everything can go into []?

You can put class name, package name or this

. Here is a relevant quote from the language specifications that explains this (see Section 5.2 for more details):

A modifier can be qualified with a C identifier (for example, private [C]), which must indicate the class or package that contains the definition. Members marked with such a modifier are accessible, respectively, only from code within package C or only from code within class C and its companion module (§5.4).

Another form of qualification is private [this]. An M element marked with this modifier is called object protected; it can only be accessed from within the object in which it is defined. That is, the choice of pM is only legal if the prefix is ​​or O. it, for some class O enclosing the link. In addition, restrictions apply to unqualified individuals.

+4


source


The first is private, for example, a class, the second for a class. If you are using the second version, you have access from another instance of the Test class (useful for a method equals

or similar).



+3


source







All Articles