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?
- 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
3 Answers
Reset to default 3Yes, 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条)