加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

斯卡拉 – varargs拼图?

发布时间:2020-12-16 18:40:08 所属栏目:安全 来源:网络整理
导读:我确定答案很简单,但我陷入了困境: Welcome to Scala version 2.7.1.final (Java HotSpot(TM) 64-Bit Server VM,Java 1.6.0_14).Type in expressions to have them evaluated.Type :help for more information.scala def f(x:Int*)=0f: (Int*)Intscala val
我确定答案很简单,但我陷入了困境:

Welcome to Scala version 2.7.1.final (Java HotSpot(TM) 64-Bit Server VM,Java 1.6.0_14).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def f(x:Int*)=0
f: (Int*)Int

scala> val xs:Seq[Int]=1::2::3::4::Nil
xs: Seq[Int] = List(1,2,3,4)

scala> f (xs)
<console>:7: error: type mismatch;
 found   : Seq[Int]
 required: Int
       f (xs)
          ^

我如何构建’Int *’?

解决方法

要将序列解压缩到参数列表中,请使用_ *

scala> f(xs: _*)
res1: Int = 0

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读