Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala private function

Tags:

scala

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

like image 219
joesan Avatar asked Dec 19 '22 04:12

joesan


2 Answers

It means Test class has a private constructor. It probably has factory methods in its companion object.

like image 160
muhuk Avatar answered Jan 05 '23 01:01

muhuk


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.

like image 27
Uwe Plonus Avatar answered Jan 05 '23 03:01

Uwe Plonus