Scala如何将Map转换为元组的varargs? [英] Scala how to convert Map to varargs of tuples?

查看:215
本文介绍了Scala如何将Map转换为元组的varargs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala Play 2.2.x测试中,我有一个Map[String, String],我需要将其传递给接受(String, String)*的函数,即(String, String)元组的varargs.

In the context of Scala Play 2.2.x testing I have a Map[String, String] which I need to pass to a function that accepts (String, String)* i.e. a varargs of (String, String) tuple.

例如

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

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

but this gives a type mismatch because withFormUrlEncodedBody accepts only a (String, String)* type.

推荐答案

简单地:

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

_* 向编译器提示的地方

这篇关于Scala如何将Map转换为元组的varargs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆