Scala:将元组解包为参数列表的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24196827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala: Unpacking tuple as part of argument list
提问by Hanxue
I am trying to send the result of a method call's tuple, as partof 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(String, (Int, Long, Long)). 如果方法返回签名与方法调用匹配,那么我可以使用
(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?
回答by Carl
You can do the following:
您可以执行以下操作:
val (result, startTime, endTime) = sendAndReceive(heartbeat)
printResult("Heartbeat only", result, startTime, endTime)
回答by kapunga
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))
回答by Alex Archambault
This can indeed be achieved without unpacking the tuple using shapeless(and tupling the function as you did):
这的确可以不用拆包元组实现不成形(和几倍的功能,像你一样):
import shapeless.syntax.std.tuple._
(printResult _).tupled("Hi" +: sendAndReceive(???))
"Hi" +: sendAndReceive(???)just prepends the value "Hi"to the tuple returned by sendAndReceive.
"Hi" +: sendAndReceive(???)只是将值添加"Hi"到sendAndReceive.
回答by elm
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)

