Couldn’t this be handled by pattern matching? Two steps would be necessary to
do so (I know that the second one is controversial, but hear me out).
First: introduce a pattern matching construct.
```js
function splice(...args) {
match(args) {
case [start, stop, ...item]:
...
case []:
...
}
...
}
```
Second: Matching an array fails if the right-hand side does not have the
correct length. Two arguments in favor of this approach:
– If you want things to be more lenient, you can always use the rest operator.
– To me (subjectively), it feels cognitively right. Matching against [x, y]
seems fundamentally different from matching against { 0: x, 1: y }. In the
former case, length and order of elements matters, in the latter case, it
doesn’t.
The neat things is that #2 would also enable the enforcement of an arity:
function foo(args) {
let [must, have, four, params] = args; // exception if arity != 4
}
On 11 Nov 2013, at 2:12 , Allen Wirfs-Brock <[email protected]> wrote:
> One of the the few remaining uses of a function's 'arguments' binding is to
> determine the actual number of passed arguments. This is necessary in some
> overloading scenarios where a function has different behavior when an
> argument is completely absent then it has when undefined (or any other
> default value) is explicitly passed in that parameter position. That
> situation occurs in a number of DOM APIs and even a few ES library functions.
>
> For example(see https://bugs.ecmascript.org/show_bug.cgi?id=1877 ),
> Array.prototype.splice returns different results for:
> [1,2,3].splice()
> and
> [1,2,3].splice(undefined)
>
> The natural ES6 declaration for a splice function is:
>
> function splice(start, deleteCount, ...items) {...
>
> but if you write it this way then within the body you have to have a test
> like:
>
> if (arguments.length == 0) {...
>
> to implement the correct web-compatable behavior.
>
> Or, alternatively you could declare the functions as:
>
> function splice(...actualArgs) {
> let [start, stop, ...item] = actualArgs;
> ...
> if (actualArgs.length == 0) {...
>
> So, to implement a Web-compaable version of splice you either have to use
> 'arguments' to determine the actual number of passed objects or you need to
> declare it with a bogus parameter pattern and use explicit or implicit
> destructuring to parse out the positional parameters.
>
> One way around this dilemma would be to provide a syntactic affordance for
> determing the actual argument count. For example, one possibility would be
> to allow the last item of any formal parameter list to be an item of the
> syntactic form:
>
> ActualArgumentCount : '#' BindingIdentifier
>
> So, the declaration for splice could then be:
>
> function splice(start, deleteCount, ...items, #argCount) {
> ...
> if (argCount == 0) {...
>
> Thoughts?
>
> Allen
--
Dr. Axel Rauschmayer
[email protected]
home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss