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
This means that the Testclass has a private constructor. There are probably factory methods in its companion object.
Test
This means a constructor private.
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.
private , . , , . -, apply ( ).
, : ().
, , case:
val t = new Test(f) // won't work val t = Test.apply(f) // works val t = Test(f)
final, override Test class.
override
Test class . i.e Test