Scala: Unpacking tuple as part of argument list

Hanxue picture Hanxue · Jun 13, 2014 · Viewed 17.2k times · Source

I am trying to send the result of a method call's tuple, as part of the argument list for another method.

Target method

def printResult(title: String, result: Int, startTime: Long, endTime: Long)

Return from method, partial argument list

def sendAndReceive(send: Array[Byte]): (Int, Long, Long)

In other words, I am trying to call printResult(String, (Int, Long, Long)). If the method return signature matches the method call, then I could have used

(printResult _).tupled(sendAndReceive(heartbeat))

This results in syntax error

printresult("Hi", Function.tupled(sendAndReceive(heartbeat))

Workaround

I am resorting to manually unpacking the tuple, then using it when calling the method

val tuple = sendAndReceive(heartbeat)
printResult("Heartbeat only", tuple._1, tuple._2, tuple._3)

Is there a more elegant way to unpack and send a tuple as part of argument list?

References

Scala: Decomposing tuples in function arguments

Invoke a method using a tuple as the parameter list

Will tuple unpacking be directly supported in parameter lists in Scala?

Tuple Unpacking in Map Operations

Answer

Carl picture Carl · Jun 13, 2014

You can do the following:

val (result, startTime, endTime) = sendAndReceive(heartbeat)
printResult("Heartbeat only", result, startTime, endTime)