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

Scala:如何将varargs指定为类型?

发布时间:2020-12-16 18:58:58 所属栏目:安全 来源:网络整理
导读:代替 def foo(configuration: (String,String)*) 我想能够写: type Configuration = (String,String)*def foo(configuration: Configuration) 主要用例是在覆盖子类时提供简单的方法签名 更新: 我可以靠近 type Param = (String,String)def foo(configurat
代替

def foo(configuration: (String,String)*)

我想能够写:

type Configuration =  (String,String)*
def foo(configuration: Configuration)

主要用例是在覆盖子类时提供简单的方法签名

更新:
我可以靠近

type Param = (String,String)
def foo(configuration: Param*)

但有没有办法做得更好?

解决方法

不,*仅允许在ParamType上,即匿名函数或方法的参数类型.

4.6.2 Repeated Parameters Syntax: ParamType ::= Type ‘’ The last value
parameter of a parameter section may
be suffixed by “
”,e.g. (…,x:T *).
The type of such a repeated parameter
inside the method is then the sequence
type scala.Seq[T ]. Methods with
repeated parameters T * take a
variable number of arguments of type T.

编译错误@Eastsun的例子是在第一行,而不是第二行.这不应该被允许:

scala> type VarArgs =  (Any*)
defined type alias VarArgs

我已经筹集了bug.

这与By-Name Parameters类似.在这种情况下,编译器会阻止创建类型别名:

scala> type LazyString = (=> String) <console>:1: error: no by-name parameter type allowed here
       type LazyString = (=> String)

你的最后一次尝试是表达这一点的标准方式.

(编辑:李大同)

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

    推荐文章
      热点阅读