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))
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?
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
You can do the following:
val (result, startTime, endTime) = sendAndReceive(heartbeat)
printResult("Heartbeat only", result, startTime, endTime)
Are you attached to this function signature?
def printResult(title: String, result: Int, startTime: Long, endTime: Long)
If it is your code and you can modify it, then you can try and use currying instead like this:
def printResult(title: String)(result: Int, startTime: Long, endTime: Long)
Then you can execute it like this:
printResult("Curried functions!") _ tupled(sendAndReceive(heartbeat))
One approach involves case classes for the tuple, for instance like this,
case class Result(result: Int, startTime: Long, endTime: Long) {
override def toString() = s"$result ($startTime to $endTime)"
}
def sendAndReceive(send: Array[Byte]): Result = {
// body
Result(1,2,3)
}
def printResult(title: String, res: Result) = println(title + res)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With