multiple return values... like ruby

Brendan Eich brendan at mozilla.com
Thu Jan 3 21:51:06 PST 2013


Anton Kovalyov wrote:
> See here: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring 
> and 
> https://developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.7#Destructuring_assignment_%28Merge_into_own_page.2Fsection%29

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).

/be

>     }
>
>     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("http://some.host.com/data");
>
>     Thanks! ;)
>
>     Vänliga hälsningar / Best regards
>     Anders Elo @ kaerus.com <http://kaerus.com>
>
>
>
>
>     _______________________________________________
>     es-discuss mailing list
>     es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
>     https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list