Scala private function

I came across a piece of code in Scala that looked like this:

final class Test private (fn: Int => Int) {
  def square(i: Int) = i * i
 }

 object Test {
   def apply(fn: Int => Int) = new Test(fn)
 }

What does it mean? I could not find references to this in the text materials that I have on Scala.

Edit: found what I wanted in this link:

https://www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch04s05.html

+4
source share
4 answers

This means that the Testclass has a private constructor. There are probably factory methods in its companion object.

+4
source

This means a constructor private.

Arguments in a class declaration are used to denote constructor arguments.

To create such an object, you can use a companion object with factory methods.

+3

private , . , , . -, apply ( ).

, : ().

, , case:

val t = new Test(f) // won't work
val t = Test.apply(f) // works
val t = Test(f)
+3

final, override Test class.

Test class . i.e Test

+1

All Articles