missing non-rest_parameters should throw error
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
-
arguments is needed for backwards compatability (no migration tax)
-
it is useful with destructing parameters:
function ({a,b,c}, [x,y]) { if (arguments.length < 2) ... ... var arg1obj = argruments[0]; ...
Sent from Samsung tablet
point 1 is utopia since ...varname breaks syntax in any case
second point would be easier like this ?
function (arg1obj={a,b,c}, p=[x,y]) {
}
On 26 October 2011 20:27, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
- arguments is needed for backwards compatability (no migration tax)
- it is useful with destructing parameters: function ({a,b,c}, [x,y]) { if (arguments.length < 2) ... ... var arg1obj = argruments[0]; ...
All languages I know with pattern matching facilities provide a simple solution for that. E.g. in ML:
fun (arg1 as {a,b,b}, arg2 as [x, y]) -> ...
We should really get rid of any need for using `arguments'. If your example is a relevant use case then supporting something along these lines in Harmony seems preferable, at least to me.
On Oct 27, 2011, at 12:04 AM, Andrea Giammarchi wrote:
point 1 is utopia since ...varname breaks syntax in any case
No, you missed the point. this need to be valid "Harmony code" in order to avoid a migration tax:
function f() { return g.apply(undefined,arguments); }
no ... or any other new syntax is involved.
second point would be easier like this ?
function (arg1obj={a,b,c}, p=[x,y]) {
}
yes, but that is ambiguous with the parameter default value syntax. If anybody wants a change in the agreed upon parameter syntax extensions they need to make a viable proposal.
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.