I know what private[this]
means in the context of a method or class variable, but what does it mean in the context of a top level class? Is it the same as private[package]
, where the this
keyword substitutes the name of the current package?
Asked
Active
Viewed 745 times
4

Gal
- 5,338
- 5
- 33
- 55
-
Yes, Check this for more details: https://stackoverflow.com/questions/9698677/privatethis-vs-private – Explorer Apr 22 '18 at 14:56
-
I saw it, but unless I'm missing something, there's no mention of the semantics for top level classes. There is no "instance" to be private to in this case (unless the package is an instance, but I've never known packages to be first level objects in Scala). – Gal Apr 22 '18 at 14:59
-
Ha! I thought that it might have to do with the synthetic object that wraps interpreted scripts, but indeed, the program `private[this] class PrivateThis` compiles with `scalac`, not just as a script. That's funny. – Andrey Tyukin Apr 22 '18 at 15:20
1 Answers
0
class Stock{
private var price: Double
def isHigh(that: Stock): Boolean = this.price > that.price
}
here that is outside object accessing private price variable But if we define
private[this] var price: Double
like this then that cant access price

Vivek
- 405
- 1
- 4
- 14