missing non-rest_parameters should throw error

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Oct 26 10:59:22 PDT 2011


I am not sure I am missing something but I think arguments could disappear
without problems, being easily simulated like this:

function f1(...arguments) {
    f2.apply(this, arguments);
    f2(...arguments);
    let [a,b,c,...rest] = arguments;
    // etc etc
}

Am I wrong ?

br

On Wed, Oct 26, 2011 at 6:07 PM, Xavier MONTILLET
<xavierm02.net at gmail.com>wrote:

> http://wiki.ecmascript.org/doku.php?id=harmony:rest_parameters
>
> Hi,
>
> Here is the behavior I would like:
>
> function f( a, b, ...rest ) { }
> f( 1, 1 );// ok
> f( 1, 1, 1 );// ok
> f( 1, undefined );// ok
> f( 1 );// Error: missing parameter
>
> Because since you can't reference a and b as properties of an object
> anymore, there is no way you can know whether they are undefined
> because the value given was undefined or because no value was given.
>
> Of course, no such error would happen if there is a default value:
>
> function f( a, b=1, ...rest ) { }
>
> ---------------------------------------------------
>
> If you consider throwing an error a bit too harsh, you should consider
> giving arguments.length. Not withing arguments but as a separate
> variable.
>
> function f( a, b, ...rest ) {
>    var awesomeNameYoullFind = f.length + rest.length; // this should
> be provided by core
>    if ( awesomeNameYoullFind < 2 ) {// and people who want to throw
> errors will have a convenient way of doing it
>        throw Error();
>    }
> }
>
> -----------------------------------------------------
>
> Another thing I think arguments shouldn't disappear. It sucks that it
> isn't an array but it still has some use-cases.
>
> e.g. a function that delegates in certain conditions
>
> function f( a, b, ...rest ) {
>    if ( a ) {
>        f2.apply( this, arguments );
>        // or f2( ...arguments );
>    } else {
>        // w/e
>    }
> }
>
> This could be done that way:
>
> function f( a, b, ...rest ) {
>    if ( a ) {
>        f2( a, b, ...rest );
>    } else {
>        // w/e
>    }
> }
>
> But what if you have a, b, c, d and so on? You don't want to copy all
> those variable names.
> And what if you want to delegate everything except a and b? Without an
> argu,ments object, it's bothersome. With it, you just slice from index
> 2...
>
> To give a real example: assertions.
>
> I use to do things like that:
>
> function f( a, b, c, d ) {
>    assertArguments( arguments, [
>        {// a
>            type: 'number'
>        },
>        {// b and c
>            type: 'string',
>            length: 2
>        },
>        {// d
>            type: 'object',
>            optionnal: true
>        }
>    ] );
> }
>
> And if you remove the arguments object, it gets really bothersome...
> You have to make sure you pass every single arguments in the good
> order...
>
> ------------------------------------------------------------
>
> But of course, it should become a real array (and probably change its
> name to args or something similar). And to prevent from breaking
> existing codes, if there is already a variable named args in the scope
> or a parent scope *and it is not the args created for another
> function*, replace it.
>
> var args = 1;
> function f( ) {
>    // here args is 1 no matter what
> }
> f(0);
>
> function f1( ) {
>    // here args is the arguments from f1
>    function f2( ) {
>        // here args is the arguments from *f2*
>        // because even though it was in the scope, some magic
> happened and the engine figured out it wasn't a variable declared by
> the user
>    }
>    f2(2);
> }
> f1(1);
>
> --------------------------------------------------------
>
> PS: If a new arguments object is created, then there is no use for
> awesomeNameYoullFind.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111026/578117cf/attachment.html>


More information about the es-discuss mailing list