Scala可变函数和Seq [英] Scala variadic functions and Seq

查看:143
本文介绍了Scala可变函数和Seq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,像 List Seq 这样的特征在Scala标准库中实现,而不是有一部分语言本身。

As far as I know, traits like List or Seq are implemented in the Scala standard library instead of being part of the language itself.

虽然有一件我不明白的东西:一种具有类似于

There is one thing that I do not understand, though: one has a syntax for variadic functions that looks like

def foo(args: String*) = ...

内部可以访问 args ,它将是一个 Seq

Internally one has access to args and it will be a Seq.

我不清楚是否:


  • Seq 被认为是足以显示为语言一部分的特殊数据结构,或

  • 这里的 * 符号是一种更通用的语法的特殊情况,可以避免任何对具体数据结构接口的引用。

  • Seq is considered a special data structure enough to appear as part of the language, or
  • the * notation here is a particular case of a more general syntax that manages to avoid any references to concrete data structures interfaces.

有谁知道哪一个是正确的解释?

Does anyone know which one is the correct intepretation?

推荐答案

这个确实有点 e和图书馆。 Scala语言规范v2.9在§4.6.2重复参数中声明:

It is indeed somewhat a 'blur' between language and library. The Scala Language Specification v2.9 states in §4.6.2 Repeated Parameters:


最后一个值参数参数部分可以后缀*,例如(..., x:T *)。方法中这样一个重复参数的类型是序列类型 scala.Seq [ T ]

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].

所以当你使用重复的参数时,假设 scala.Seq 在运行时可用(应该是这种情况,因为它是标准库的一部分)。

So when you use repeated arguments, it is assumed that scala.Seq is available at runtime (which should be the case, as it is part of the standard library).

这篇关于Scala可变函数和Seq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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