Jija
Jija

Reputation: 1015

Protected functions in Scala

How do you make a protected function in a class accessible to just all classes in the package in Scala?

Upvotes: 15

Views: 6631

Answers (2)

Jija
Jija

Reputation: 1015

You can do

protected[packagename] def foo() {...}

Upvotes: 40

mkneissl
mkneissl

Reputation: 4912

If we pay attention to the distinction between function and method, we can define an object deriving from Function:

protected[packagename] object fn extends (Int=>Int) {
  def apply(n: Int) = 2*n 
}

Upvotes: 11

Related Questions