multiple return values... like ruby
Anton Kovalyov wrote:
anders elo wrote:
I'd like to propose the incorporation of multiple return values into the ES standard. function foo(){ return 1,2,3; } let (a,b,c) = foo(); let (a,b) = foo(); // ignore c let (a,...b) = foo() // a = 1, b = [2,3] /* also useful for asynchronous functions returning a promise */ let (response,headers) = yield getAjaxData("some.host.com/data");
See here: harmony:destructuring and developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.7#Destructuring_assignment_(Merge_into_own_page.2Fsection)
Anton
Hi Anders,
The syntax you proposed is not backwards compatible.
Anton has directed you to articles on destructuring, which is the ES6 way to accomplish what you want.
Let me elaborate. Your examples can be accomplished as follows:
function foo() { // We return an array of values. return [ 1, 2, 3 ]; }
let [ a, b, c ] = foo();
let [ a, b ] = foo(); // c is ignored
let [ a, ...b ] = foo(); // a is 1, b is [ 2, 3 ]
I believe if you are using Task.js, you can additionally use the asynchronous form in like manner.
Nathan
Anton Kovalyov wrote:
See here: harmony:destructuring and developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.7#Destructuring_assignment_(Merge_into_own_page.2Fsection)
Right -- here are inline examples:
function pair(x, y) { return [x, h]; } let [x, y] = pair(3,4);
This can be done without allocation with an optimization from Scheme implementations, where the JIT looks into the continuation and sees the destructuring of a fresh array result, and avoids creating a temporary array just to pull it apart.
Anton
On 3 January 2013 07:26, anders elo <anders at kaerus.com <mailto:anders at kaerus.com>> wrote:
I'd like to propose the incorporation of multiple return values into the ES standard. function foo(){ return 1,2,3;
Note that this is not backward compatible because JS inherited the comma operator from C (unlike Java -- recessive gene skipped a generation :-P).
Brendan Eich wrote:
function pair(x, y) { return [x, h]; }
s/h/y/ there of course!
let [x, y] = pair(3,4);
And Nathan beat me to it -- sorry for not updating mail before replying...
Thank you guys, I somehow missed that vital piece of information... Right after I sent the proposal I read Brendans blog post from October and found the info regarding returning arrays. Sorry for the noise. :) I'll read the specs more thoroughly and get back if I have any more concerns.
Anyhow I'll just drop my remaining top wishes for JS.
- To have something like a static var for prototypes (or private if you prefer OO terminology).
- Polymorphic functions, same name different signatures. Erlang matchers are cool btw.
- keep it simple and clean looking (not like C++ or Java). Code should be readable / self documenting.
That's about it!
I'd like to propose the incorporation of multiple return values into the ES standard.
function foo(){ return 1,2,3; }
let (a,b,c) = foo();
let (a,b) = foo(); // ignore c
let (a,...b) = foo() // a = 1, b = [2,3]
/* also useful for asynchronous functions returning a promise */ let (response,headers) = yield getAjaxData("some.host.com/data");
Thanks! ;)
Vänliga hälsningar / Best
Anders Elo @ kaerus.com