I came across a code snippet 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 would that mean? I could not find any 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
It means Test
class has a private constructor. It probably has factory methods in its companion object.
This means that the constructor is private
.
The arguments in the class declaration are used to denote the constructor arguments.
To create such an object the companion object with factory methods can be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With