我正在尝试将一个方法调用返回的元组作为另一个方法参数列表中的一部分发送。
目标方法
def printResult(title: String, result: Int, startTime: Long, endTime: Long)
从方法返回,部分参数列表
def sendAndReceive(send: Array[Byte]): (Int, Long, Long)
换句话说,我试图调用printResult(String, (Int, Long, Long))
。如果方法的返回签名与方法调用匹配,那么我可以使用。(printResult _).tupled(sendAndReceive(heartbeat))
这会导致语法错误
printresult("Hi", Function.tupled(sendAndReceive(heartbeat))
解决方法
我采用手动解包元组的方式,在调用该方法时使用。
val tuple = sendAndReceive(heartbeat)
printResult("Heartbeat only", tuple._1, tuple._2, tuple._3)
有没有更优雅的方式来解包并将元组作为参数列表的一部分发送?
参考资料
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?
invokeMethod(arg1, arg2, tuple)
这样的事情。 - Hanxue