Private scoping with square brackets (private[…]) in Scala

前端 未结 2 2094
太阳男子
太阳男子 2020-12-28 14:17

I\'ve come across the following syntax while looking through the Gatling source code:

private[http] def build = {
  // ...
}

What is the sy

相关标签:
2条回答
  • 2020-12-28 14:47

    See the scala reference, specifically, chapter 5.2. Some excerpt:

    The private modifier can be used with any definition or declaration in a template. Such members can be accessed only from within the directly enclosing template and its companion module or companion class (§5.4). They are not inherited by subclasses and they may not override definitions in parent classes.

    The modifier can be qualified with an identifier C (e.g. private[C]) that must denote a class or package enclosing the definition. Members labeled with such a modifier are accessible respectively only from code inside the package C or only from code inside the class C and its companion module (§5.4). Such members are also inherited only from templates inside C.

    0 讨论(0)
  • 2020-12-28 15:00

    In short: this is used for scope protection:

    • private[C] means that access is private "up to" C, where C is the corresponding package, class or singleton object.

    Same to protected[C]

    • protected[C]: access is protected "up to" C, where C is the corresponding package, class or singleton object.
    0 讨论(0)
提交回复
热议问题