I have a Scala class with two parameters, and another one parameter constructor. For the one parameter constructor, I call a method to get a tuple of two elements and tried to use the tuple for the parameter of the constuctor that requires two parametes. This is an example:
def vals(v:Int) = {
// computation
(v,v) // returns two element tuple
}
class A(a:Int, b:Int) {
def this(v:Int) = {
this(vals(v))
}
}
object Main extends App {
val a = new A(10)
}
However, I get type mismatch error. I found a solution in scala tuple unpacking that works with function invocation, but not with constructor.
def foo(x: Int, y: Int) = x * y
def getParams = {
(1,2) //where a & b are Int
}
object Main extends App {
println((foo _).tupled(getParams))
println(Function.tupled(foo _)(getParams))
}
How can solve this issue?