Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between private modifier position in a Scala class declaration?

Tags:

scala

Suppose I have two classes:

private[example] class PoorInt    extends Int
class RichInt    private[example] extends Int

The question is what's the difference between private modifier position in these classes declarations?

like image 550
Finkelson Avatar asked Nov 17 '25 07:11

Finkelson


1 Answers

The first refers to the scope of the class, i.e. you cannot access PoorInt outside package example.

The second refers to the scope of the constructor of RichInt, which you are not providing explicitly, i.e. you cannot use its constructor outside package example.

For example:

// somewhere outside package example ...
val x = new RichInt // doesn't compile. Constructor is private
val y : PoorInt = ... // doesn't compile. PoorInt is not accessible from here
def f(x: PoorInt) = ... // doesn't compile. PoorInt is not accessible from here

You can also see this question.

like image 158
ale64bit Avatar answered Nov 19 '25 20:11

ale64bit