【发布时间】:2014-08-03 12:17:06
【问题描述】:
我正在尝试发送方法调用元组的结果,作为另一个方法的参数列表的部分。
目标方法
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?
【问题讨论】:
标签: scala tuples iterable-unpacking arity