gpt4 book ai didi

Scala如何将Map转换为元组的可变参数?

转载 作者:行者123 更新时间:2023-12-01 08:58:29 25 4
gpt4 key购买 nike

在 Scala Play 2.2.x 测试的上下文中,我有一个 Map[String, String] 我需要将其传递给一个接受 (String, String)* 的函数code> 即 (String, String) 元组的可变参数。

例如

val data : Map[String, String] = Map("value" -> "25", "id" -> "", "columnName" -> "trades")
route(FakeRequest(POST, "/whatever/do").withFormUrlEncodedBody(data))

但这会导致类型不匹配,因为 withFormUrlEncodedBody 只接受 (String, String)* 类型。

最佳答案

简单地说:

def foo(names: (String, String)*) = names.foreach(println)
val folks = Map("john" -> "smith", "queen" -> "mary")
foo(folks.toSeq:_*)
// (john,smith)
// (queen,mary)

在哪里 _* is a hint to compiler .

关于Scala如何将Map转换为元组的可变参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24012146/

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