最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does JS's spread syntax appear in other languages? - Stack Overflow

programmeradmin0浏览0评论

I first encountered the spread (...) syntax in JavaScript, and have grown to appreciate the many things it can do, but I confess I still find it quite bizarre. Is there an equivalent in other languages, and what is it called there?

I first encountered the spread (...) syntax in JavaScript, and have grown to appreciate the many things it can do, but I confess I still find it quite bizarre. Is there an equivalent in other languages, and what is it called there?

Share edited Sep 7, 2018 at 0:07 Patrick Roberts 52.1k10 gold badges117 silver badges163 bronze badges asked May 4, 2018 at 16:26 Danyal AytekinDanyal Aytekin 4,2153 gold badges39 silver badges44 bronze badges 3
  • 1 Groovy has a spread operator: groovy-lang/operators.html#_spread_operator. It's functionality is quite different though. – user47589 Commented May 4, 2018 at 16:29
  • 1 To be precise, the spread syntax is not an "operator". The term "operator" has a specific meaning in the expression grammar, and the spread syntax isn't part of that. – Pointy Commented May 4, 2018 at 16:33
  • 1 The Go language allows this as a means of implementing variadic functions. I prefer its form, where in the "receiving" position (parameters), the ... es before the type ident, and in the "sending" positions (arguments), it es after the values. func foo(bar string, rest ...string) { /***/ } ... foo("bar", myStrings...) – user2437417 Commented May 4, 2018 at 16:41
Add a ment  | 

3 Answers 3

Reset to default 3

Yes, in Ruby: the splat operator. It's an asterisk instead of three dots:

def foo(a, *b, **c)
   [a, b, c]
end

> foo 10
=> [10, [], {}]
> foo 10, 20, 30  
=> [10, [20, 30], {}] 
> foo 10, 20, 30, d: 40, e: 50
=> [10, [20, 30], {:d=>40, :e=>50}]
> foo 10, d: 40, e: 50
=> [10, [], {:d=>40, :e=>50}]

(Copied from this answer)

Common Lisp has &rest parameters:

(defun do-something (&rest params) 
    ...
)

PHP has it too, using the three dots: it is a new feature of PHP version 5.6. The operator looks like a ellipsis (…) character but it is not.

发布评论

评论列表(0)

  1. 暂无评论