Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JS's spread syntax appear in other languages?

Tags:

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?

like image 226
Danyal Aytekin Avatar asked May 04 '18 16:05

Danyal Aytekin


People also ask

What does spread syntax do?

Spread syntax ( ... ) allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.

Does spread operator pass reference?

Spread allows you to make a shallow copy of an array or object, meaning that any top level properties will be cloned, but nested objects will still be passed by reference. For simple arrays or objects, a shallow copy may be all you need.

What is the difference between rest and spread operator?

The main difference between rest and spread is that the rest operator puts the rest of some specific user-supplied values into a JavaScript array. But the spread syntax expands iterables into individual elements.

How does the spread operator work in JavaScript?

Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 values are expected. It allows us the privilege to obtain a list of parameters from an array.


1 Answers

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)

like image 142
kwyntes Avatar answered Sep 28 '22 17:09

kwyntes