user3607625 user3607625 - 1 month ago 5
Scala Question

How to pass a tuple3 as an argument to function?

I'm trying to pass a tuple as an argument to function. Unfortunately i can't do this. Can you give me some tips?

val t = Tuple3(3, "abc", 5.5);
def fun(x: (Int, String, Double) = {
x.productIterator.foreach(i => println("Value: " + i));
}
def(t);

Answer

There's a missing closing parenthese and you called def(t) instead of fun(t). Note that you don't need to indicate the constructor Tuple3 :

val t = (3, "abc", 5.5);
def fun(x: (Int, String, Double)) = {
  x.productIterator.foreach(i => println("Value: " + i));
}
fun(t);