Im learning Scala now, im trying to pass a tuple to a function that prints all elements with for loop. This is what ive done so far but obviously something went wrong.
object Tuple_demo {
def main(args: Array[String])
{
var tup1=(10,"test",6.8)
printMe(tup1)
}
def printMe(tup1:Tuple1)
{
for (ob<-tup1)
{
println(ob)
}
}
}
All Scala Tuples extend Product. You can use its
productIteratorto iterate over tuple items:The type declaration of your
printMefunction is incorrect. It should be a 3-ary tuple with types of its items specified, i.e.tup1: Tuple3[Int, String, Double]. Also, Scala has a sugar for tuple type declarations, so the following would also be correct:tup1: (Int, String, Double).