gpt4 book ai didi

Scala:将元组解包为参数列表的一部分

转载 作者:行者123 更新时间:2023-12-03 13:16:09 24 4
gpt4 key购买 nike

我正在尝试发送方法调用元组的结果,如 部分 另一个方法的参数列表。

目标方法

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?

Tuple Unpacking in Map Operations

最佳答案

您可以执行以下操作:

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

关于Scala:将元组解包为参数列表的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24196827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com