突破到特定集合类型的捷径? [英] short way to breakOut to specific collection type?

查看:33
本文介绍了突破到特定集合类型的捷径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scala> val m = Map(1 -> 2)
m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)

scala>  m.map{case (a, b) => (a+ 1, a+2, a+3)}
res42: scala.collection.immutable.Iterable[(Int, Int, Int)] = List((2,3,4))

我想要的结果类型是 List[(Int, Int, Int)].我发现的唯一方法是:

What I want is for the result type to be List[(Int, Int, Int)]. The only way I found is:

scala>  m.map{case (a, b) => (a+ 1, a+2, a+3)}(breakOut[Map[_,_], (Int, Int, Int), List[(Int, Int, Int)]])
res43: List[(Int, Int, Int)] = List((2,3,4))

有更短的方法吗?

推荐答案

您可以通过让 breakOut 的类型参数从返回类型中推断出来,使其更加简洁:

You can make it a bit more concise by letting the type parameters to breakOut be inferred from the return type:

scala>  m.map{case (a, b) => (a+1, a+2, a+3)}(breakOut) : List[(Int, Int, Int)]
res3: List[(Int, Int, Int)] = List((2,3,4))

这篇关于突破到特定集合类型的捷径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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