Enforcing arity

2013-03-16 Thread Axel Rauschmayer
What is the recommended way of enforcing arity under ECMAScript 6 (while 
avoiding arguments.length)?

Two ideas:

1. Parameter default value is a function call that throws an exception. Not 
sure if that works.
function foo(required = throwAnException(), optional) { ... }

2. Introduce a postfix `!` matching operator, the inverse of postfix `?`:
function foo(required!, optional) { ... }

-- 
Dr. Axel Rauschmayer
a...@rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity

2013-03-16 Thread Allen Wirfs-Brock
If you need to do arity analysis of parameter but also what to apply default 
values, destructuring, etc I would do the following:

Instead of
   function ([a,b], c,d=5, ...rest) {...}
do
   function (...args) {
   if (args.length 3) throw Error(too few arguments);
   let [[a,b], c,d=5, ...rest] = args;  //probably need some ?'s in there, 
but I haven't internalized the details of the new pattern matching yet.
   }

Allen



On Mar 16, 2013, at 5:02 PM, Axel Rauschmayer wrote:

 What is the recommended way of enforcing arity under ECMAScript 6 (while 
 avoiding arguments.length)?
 
 Two ideas:
 
 1. Parameter default value is a function call that throws an exception. Not 
 sure if that works.
 function foo(required = throwAnException(), optional) { ... }
 
 2. Introduce a postfix `!` matching operator, the inverse of postfix `?`:
 function foo(required!, optional) { ... }
 
 -- 
 Dr. Axel Rauschmayer
 a...@rauschma.de
 
 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com
 
 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity

2013-03-16 Thread Axel Rauschmayer
On Mar 17, 2013, at 1:17 , Allen Wirfs-Brock al...@wirfs-brock.com wrote:

 If you need to do arity analysis of parameter but also what to apply default 
 values, destructuring, etc I would do the following:
 
 Instead of
function ([a,b], c,d=5, ...rest) {...}
 do
function (...args) {
if (args.length 3) throw Error(too few arguments);  // (*)
let [[a,b], c,d=5, ...rest] = args;  //probably need some ?'s in 
 there, but I haven't internalized the details of the new pattern matching yet.
}


Nice, I forgot about rest parameters. You’d still get an exception from 
matching if you omitted line (*), right?
Shame that we can’t have the destructuring semantics for parameters.

-- 
Dr. Axel Rauschmayer
a...@rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity

2013-03-16 Thread Kevin Gadd
Would using (...args) incur a performance penalty and impair
optimization since the argument list has to be an array now? Or is
that still better than using 'arguments.length'? Enforcing arity is a
common enough (and important, IMO) pattern that I'd be wary of doing
it using a pattern that is going to slow everything down. Something
that makes every call create garbage seems like a likely candidate to
do that.

-kg

On Sat, Mar 16, 2013 at 5:24 PM, Axel Rauschmayer a...@rauschma.de wrote:
 On Mar 17, 2013, at 1:17 , Allen Wirfs-Brock al...@wirfs-brock.com wrote:

 If you need to do arity analysis of parameter but also what to apply default
 values, destructuring, etc I would do the following:

 Instead of
function ([a,b], c,d=5, ...rest) {...}
 do
function (...args) {
if (args.length 3) throw Error(too few arguments);  // (*)
let [[a,b], c,d=5, ...rest] = args;  //probably need some ?'s in
 there, but I haven't internalized the details of the new pattern matching
 yet.
}


 Nice, I forgot about rest parameters. You’d still get an exception from
 matching if you omitted line (*), right?
 Shame that we can’t have the destructuring semantics for parameters.

 --
 Dr. Axel Rauschmayer
 a...@rauschma.de

 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com


 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity

2013-03-16 Thread Allen Wirfs-Brock
It's certainly plausible that an implementation could do the analysis to 
recognize this pattern and implement it so there isn't an extra allocation or 
any extra copying.

It it became an common perf. bottleneck you might expect the better 
implementation to do so.  If it isn't a common perf. issue, then it really 
doesn't matter.

Allen



On Mar 16, 2013, at 5:27 PM, Kevin Gadd wrote:

 Would using (...args) incur a performance penalty and impair
 optimization since the argument list has to be an array now? Or is
 that still better than using 'arguments.length'? Enforcing arity is a
 common enough (and important, IMO) pattern that I'd be wary of doing
 it using a pattern that is going to slow everything down. Something
 that makes every call create garbage seems like a likely candidate to
 do that.
 
 -kg
 
 On Sat, Mar 16, 2013 at 5:24 PM, Axel Rauschmayer a...@rauschma.de wrote:
 On Mar 17, 2013, at 1:17 , Allen Wirfs-Brock al...@wirfs-brock.com wrote:
 
 If you need to do arity analysis of parameter but also what to apply default
 values, destructuring, etc I would do the following:
 
 Instead of
   function ([a,b], c,d=5, ...rest) {...}
 do
   function (...args) {
   if (args.length 3) throw Error(too few arguments);  // (*)
   let [[a,b], c,d=5, ...rest] = args;  //probably need some ?'s in
 there, but I haven't internalized the details of the new pattern matching
 yet.
   }
 
 
 Nice, I forgot about rest parameters. You’d still get an exception from
 matching if you omitted line (*), right?
 Shame that we can’t have the destructuring semantics for parameters.
 
 --
 Dr. Axel Rauschmayer
 a...@rauschma.de
 
 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com
 
 
 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss
 
 

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity?

2013-01-07 Thread Jason Orendorff
On Sun, Jan 6, 2013 at 6:31 PM, Axel Rauschmayer a...@rauschma.de wrote:

 What is the simplest way of enforcing an arity in ES6? Doesn’t it involve
 arguments?


Hmm. Can you do this?

function f(x, y, ...[]) {}

-j
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity?

2013-01-07 Thread Brendan Eich

Jason Orendorff wrote:
On Sun, Jan 6, 2013 at 6:31 PM, Axel Rauschmayer a...@rauschma.de 
mailto:a...@rauschma.de wrote:


What is the simplest way of enforcing an arity in ES6? Doesn’t it
involve arguments?


Hmm. Can you do this?

function f(x, y, ...[]) {}


The question is, with refutable-by-default, does the empty array pattern 
fail on any non-empty arraylike match target? If yes, then we need a way 
to match prefixes of arraylikes (including empty prefixes!):


  let [] = must_be_empty();
  let [...many] = however_many();
  let [first?, second?] = two();

I buy it!

/be
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity?

2013-01-07 Thread Axel Rauschmayer
I’m torn: On one hand, I love the cleverness of it and it is certainly a good 
solution (and one that doesn’t necessitate introducing new language features).

On the other hand, I’m wondering if it wouldn’t be cleaner to have a 
per-function flag that tells JavaScript to enforce the specified arity (which 
can be done quite precisely, thanks to ES6’s advanced parameter handling). One 
possibility for setting such a flag are Python-style decorators:
http://www.python.org/dev/peps/pep-0318/

Axel

On Jan 7, 2013, at 20:33 , Brendan Eich bren...@mozilla.com wrote:

 Jason Orendorff wrote:
 On Sun, Jan 6, 2013 at 6:31 PM, Axel Rauschmayer a...@rauschma.de 
 mailto:a...@rauschma.de wrote:
 
What is the simplest way of enforcing an arity in ES6? Doesn’t it
involve arguments?
 
 
 Hmm. Can you do this?
 
function f(x, y, ...[]) {}
 
 The question is, with refutable-by-default, does the empty array pattern fail 
 on any non-empty arraylike match target? If yes, then we need a way to match 
 prefixes of arraylikes (including empty prefixes!):
 
  let [] = must_be_empty();
  let [...many] = however_many();
  let [first?, second?] = two();
 
 I buy it!
 
 /be
 

-- 
Dr. Axel Rauschmayer
a...@rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity?

2013-01-06 Thread Herby Vojčík



Axel Rauschmayer wrote:

What is the simplest way of enforcing an arity in ES6? Doesn’t it
involve arguments?

function add(x, y) {
if (arguments.length !== 2) throw ...
}

To avoid `argument`, one could:
- ensure a maximum arity by adding a ...rest parameter and checking that
its length is 0.
- ensure a minimum arity, by giving y a default value and checking for it.

Would fail-fast destructuring work?

function add(...args) {
let [x,y] = args;
}


And it this would be possible, it could be in the signature:

  function add(...[x,y,opt?]) {
  }


--
Dr. Axel Rauschmayer
a...@rauschma.de mailto:a...@rauschma.de


Herby
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Enforcing arity?

2013-01-06 Thread Rick Waldron
On Sunday, January 6, 2013, Axel Rauschmayer wrote:

 What is the simplest way of enforcing an arity in ES6? Doesn’t it involve
 arguments?

 function add(x, y) {
 if (arguments.length !== 2) throw ...
 }


 To avoid `argument`, one could:
 - ensure a maximum arity by adding a ...rest parameter and checking that
 its length is 0.


I can't find the thread, but a similar question was asked sometime in the
last 6 months (I think??) and this was the answer that was ultimately
agreed on as the best way to check arity length.


- ensure a minimum arity, by giving y a default value and checking for it.


Explicit undefined arguments, intentional or not, would trigger the default
value.

Rick



 Would fail-fast destructuring work?

 function add(...args) {
 let [x,y] = args;
 }

 --
 Dr. Axel Rauschmayer
 a...@rauschma.de javascript:_e({}, 'cvml', 'a...@rauschma.de');

 home: rauschma.de
 twitter: twitter.com/rauschma
 blog: 2ality.com


___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss