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

I first encountered the spread (...) syntax in JavaScript, and have grown to appreciate the many things

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098339a4611120.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信