ECMAScript 4 Netscape Proposal
Rationale
Named Function Parameters
previousupnext

Wednesday, January 29, 2003

Named function parameters had originally been part of the ECMAScript 4 proposal but were deferred to a future version of the language in order to keep ECMAScript 4 small. If implemented, named function parameters might behave as described in this section.

Overview

The named function parameter extension allows some function parameters to be passed by name instead of by position. Parameter names are simple strings. A function with three positional (one of which is optional) and four named parameters might be declared as:

function f(a: Number, b: Boolean, c: Number = 5, named x: Integer = 7, named y = null, named z = 34, named t = undefined)

Such a function can then be invoked as follows:

f(2, true, y: 5);
f(2, true, 8, z: 32, x: 9);

Named parameters are always optional and must include default values. A function call may specify positional arguments followed by named arguments. Positional parameters can only match positional arguments. Named parameters can only match named arguments. The same parameter may not be both positional and named.

The following sections explain the changes to add named function parameters in more detail.

Lexer

A new non-reserved keyword named named is added. This keyword is a valid identifier:

Identifier 
   Identifier
|  get
|  set
|  exclude
|  include
|  named

Function Parameter Lists

A function may take zero or more positional parameters followed by either an optional rest parameter followed by zero or more named parameters or a named rest parameter:

Parameters 
   «empty»
|  NonemptyParameters
NonemptyParameters 
   ParameterInit
|  ParameterInit , NonemptyParameters
|  RestAndNamedParameters
RestAndNamedParameters 
   NamedParameters
|  RestParameter
|  RestParameter , NamedParameters
|  NamedRestParameter
NamedParameters 
   NamedParameter
|  NamedParameter , NamedParameters

Individual parameters have the forms:

ParameterCore  TypedIdentifierallowIn
Parameter 
   ParameterCore
|  const ParameterCore
ParameterInit 
   Parameter
|  Parameter = AssignmentExpressionallowIn
NamedParameterCore  TypedIdentifierallowIn = AssignmentExpressionallowIn
NamedParameter 
   named NamedParameterCore
|  const named NamedParameterCore
|  named const NamedParameterCore
RestParameter 
   ...
|  ... Parameter
NamedRestParameter 
   ... named Identifier
|  ... const named Identifier
|  ... named const Identifier

If a Parameter is followed by a =, then that parameter is optional. If a function call does not provide an argument for an optional parameter, then that parameter is set to the value of its AssignmentExpression, implicitly coerced to the parameter’s type if necessary.

If a parameter is prefixed with named, then the parameter is matched by name rather than by position. Named parameters are always optional and must have initializers.

Attempting to define a function with two different parameters with the same name is an error.

A function with named parameters is never unchecked.

Rest Parameter

If the ... is present, the function accepts arguments not matched by any of the other listed parameters. If a parameter is given after the ..., then that parameter’s identifier is bound to an array of all remaining arguments. That identifier is declared as a local const using the parameter’s type, which defaults to Array. If the rest parameter is named, then the parameter’s type is always Array and cannot be specified explicitly.

The remaining positional arguments are stored as elements of the rest array with numeric indices starting from 0. If the rest parameter is not named, then there must be no remaining named arguments. Otherwise, extra named arguments are allowed and are stored as named properties of the rest array.

Call Processing

When a function is called, the following list indicates the order of evaluation of the various expressions in a FunctionDefinition. These steps are taken only after all of the argument names and values have been evaluated.

  1. If the function is unchecked, bind the arguments local variable to an array of all arguments and their names.
  2. Get the saved type t that was the result of evaluating the first parameter’s TypeExpression at the time the function was defined.
  3. If the first parameter is required and no positional argument has been supplied for it, then raise an error unless the function is unchecked, in which case let undefined be the first parameter’s value.
  4. If the first parameter is optional and there is a positional argument remaining, use the value of the positional argument. If there are no remaining positional arguments, then evaluate the first parameter’s AssignmentExpression and let it be the first parameter’s value.
  5. If the first parameter is named and there is a named argument with a matching argument name, then use the value of that named argument. Otherwise, evaluate the first parameter’s AssignmentExpression and let it be the first parameter’s value.
  6. Implicitly coerce the argument (or default) value to type t and bind the parameter’s Identifier to the result.
  7. Repeat steps 2-6 for each additional required, optional, and named parameter.
  8. If there is a RestParameter and one or more of the remaining arguments is named, raise an error.
  9. If there is a RestParameter with an Identifier, bind that Identifier to an array of the remaining positional arguments using indices starting from 0.
  10. If there is a NamedRestParameter, bind its Identifier to an array of the remaining positional arguments using indices starting from 0 as well as the remaining named arguments stored using named properties.
  11. If there is no RestParameter or NamedRestParameter and any arguments remain, raise an error unless the function is unchecked.
  12. Evaluate the body.
  13. Get the saved type r that was the result of evaluating the result TypeExpression at the time the function was defined.
  14. Implicitly coerce the result to type r and return it.

Function Calls

The a[args] and f(args) indexing and function call operators are extended to allow named arguments:

Brackets 
   [ ]
|  [ ListExpressionallowIn ]
|  [ NamedArgumentList ]
Arguments 
   ParenExpressions
|  ( NamedArgumentList )
ParenExpressions 
   ( )
|  ParenListExpression
NamedArgumentList 
   LiteralField
|  ListExpressionallowIn , LiteralField
|  NamedArgumentList , LiteralField

A list of arguments can contain both positional and named arguments. The positional arguments are the subexpressions separated by commas within the ListExpressionallowIn. Named arguments use the same syntax as object literals. All strings except those consisting entirely of digits are legal for argument names. No two arguments may have the same name. Named arguments must follow positional arguments, but otherwise the order of named arguments is immaterial.


Waldemar Horwat
Last modified Wednesday, January 29, 2003
previousupnext