Scala variadic functions and Seq

℡╲_俬逩灬. 提交于 2019-12-06 18:33:26

问题


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*) = ...

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

It is not clear to me whether:

  • 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?


回答1:


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

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

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




回答2:


I think it is the first one. There are a couple of types that the language demands to exist although they aren't really part of the language. With Seq you found one.



来源:https://stackoverflow.com/questions/11931851/scala-variadic-functions-and-seq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!