Are function arguments with the value `undefined` "present"?

# Till Schneidereit (11 years ago)

I couldn't find any precise definition for the spec term "present", so this is not entirely clear to me.

Example: In step 7 of 15.4.3.21 Array.prototype.reduce, a TypeError must be thrown if the optional argument initialValue isn't present. If a value of undefined does not cause the argument to be present, that function can be implemented with the following signature:

function ArrayReduce(callbackfn, initialValue = undefined)

If, however, the value undefined means that the argument is present, the signature should probably be:

function ArrayReduce(callbackfn, ...rest)

The question isn't entirely academic, as I'm trying to get the self-hosted implementations of builtins in SpiderMonkey to be as close to the spec as possible.

# David Bruant (11 years ago)

I believe Boris Zbarski touched on this recently at lists.w3.org/Archives/Public/public-script-coord/2013AprJun/0428.html (I haven't read the full thread, so can't tell what the conclusion were, but it's probably worth checking out)

# Till Schneidereit (11 years ago)

ah, thanks. That is indeed what I was looking for. Sadly, it isn't entirely clear to me that any sort of real conclusion has been reached.

# Rick Waldron (11 years ago)

On Fri, May 31, 2013 at 7:33 AM, Till Schneidereit <tschneidereit at gmail.com> wrote:

I couldn't find any precise definition for the spec term "present", so this is not entirely clear to me.

Example: In step 7 of 15.4.3.21 Array.prototype.reduce, a TypeError must be thrown if the optional argument initialValue isn't present. If a value of undefined does not cause the argument to be present, that function can be implemented with the following signature:

function ArrayReduce(callbackfn, initialValue = undefined)

undefined is a valid initial value for Array.prototype.reduce. undefined will also trigger the default parameter, so this works until nothing is provided, which breaks Step 10.

If, however, the value undefined means that the argument is present, the signature should probably be:

function ArrayReduce(callbackfn, ...rest)

With minor changes to steps 9 and 10, plus the addition of steps to check rest.length and then Get(rest, "0"), this would work. I suspect that Allen would prefer to craft some sort of spec mechanism, from this use case, for "is foo present" assertions throughout the spec.

# Till Schneidereit (11 years ago)

On Fri, May 31, 2013 at 5:01 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

undefined is a valid initial value for Array.prototype.reduce. undefined will also trigger the default parameter, so this works until nothing is provided, which breaks Step 10.

I guess that's what my question boils down to: is undefined a valid initial value? It probably has to be for backwards-compatibility reasons.

If, however, the value undefined means that the argument is present, the signature should probably be:

function ArrayReduce(callbackfn, ...rest)

With minor changes to steps 9 and 10, plus the addition of steps to check rest.length and then Get(rest, "0"), this would work. I suspect that Allen would prefer to craft some sort of spec mechanism, from this use case, for "is foo present" assertions throughout the spec.

That's what I'm using for the moment, and will probably continue using, given the abovementioned conclusion regarding backwards-compatibility, yes.

thanks, till

# Allen Wirfs-Brock (11 years ago)

If a term is not explicitly defined by the spec. then the English dictionary definition applies: "4) being with one or others or in the specified or understood place"

This is further clarified by the introductory paragraphs of chapter 15 that say: "Unless otherwise specified...if a function or constructor described in this clause is given fewer arguments than the function is specified to require, the function or constructor shall behave exactly as if it had been given sufficient additional arguments, each such argument being the undefined value."

Testing is an argument is present is an "otherwise specified" situation. In other words, of an argument is not present then it is treated as if the value is undefined. In general, the specification test for undefined as an argument position if it doesn't care whether or not an explicit undefined was passed.

Any time the spec. says "if argument foo is present..." it could be restate by "if the number of explicitly passed arguments is >= foo's argument position..."

On May 31, 2013, at 4:33 AM, Till Schneidereit wrote:

I couldn't find any precise definition for the spec term "present", so this is not entirely clear to me.

Example: In step 7 of 15.4.3.21 Array.prototype.reduce, a TypeError must be thrown if the optional argument initialValue isn't present. If a value of undefined does not cause the argument to be present, that function can be implemented with the following signature:

function ArrayReduce(callbackfn, initialValue = undefined)

If, however, the value undefined means that the argument is present, the signature should probably be:

function ArrayReduce(callbackfn, ...rest)

or you could define it as:

const missing = {};
function ArrayReduce(callbackfn, initValue=missing) {
   ....
   //step 7
   if (len === 0 && initialValue === missing) throw new TypeError("initial value required");

The question isn't entirely academic, as I'm trying to get the self-hosted implementations of builtins in SpiderMonkey to be as close to the spec as possible.

that better be semantically identical to the spec...