In Scala, a helper constructor with multiple parameters… here is a solution to the problem.
In Scala, a helper constructor with multiple parameters
I have a class initialized with integers and collections. I want to create a helper constructor with integers and multiple arguments. These multiple parameters should be the contents of the collection.
I need help calling the main constructor with the appropriate parameters.
final class Soccer[A](max: Int, collection_num: Set[A]) {
Auxiliary Constructor
new Soccer(n,A,B,C)` is equivalent to `new Soccer(n,Set(A,B,C))`.
This constructor requires at least two candidates in order to avoid ambiguities with the
primary constructor.
def this(max: Int, first: A, second: A, other: A*) = {
I need help calling the primary constructor with suitable arguments.
}
}
New Soccer(n,A,B,C
) should be equivalent to
new Soccer(n,Set(A,B,C)).
Solution
Try defining factory apply method on the companion object instead of a secondary constructor like this
object Soccer {
def apply[A](max: Int, first: A, second: A, other: A*): Soccer[A] = {
new Soccer(max, Set[A](first, second) ++ other.toSet)
}
}
Now build Soccer
like this
Soccer(n,A,B,C)