Re: [WebIDL] Default Values for Arguments

On Mon, Nov 30, 2009 at 2:55 PM, Ian Hickson <ian@hixie.ch> wrote:
> On Fri, 27 Nov 2009, Steffen Krüssel wrote:
>>
>> If I understand you right, it should be enough to put a descriptive text
>> on the appropriate method in order to indicate what the actual
>> requirements for an implementation are (e.g. default value if left out)?
>>
>> But then most of the implementation-relevant requirements could be put
>> into the interface's documentation rather than specified formally. For
>> example, [TreatNullAs] could also be documented informal, can't it? So
>> if I didn't get anything wrong, the question is which directives are
>> specified (in)formal?!
>
> Yes, [TreatNullAs] could also be handled this way. In fact, that's how I
> generally handle that particular case.
>
> We could add syntax for default values also, I don't have a strong opinion
> either way. I was just trying to clarify the earlier e-mail.

In gecko we give all default arguments a default value. For booleans
the default is false, for strings the empty string, for numbers 0, and
for objects null. I only know of three interfaces where these rules
aren't enough and we have to add specific code to implement different
behavior:

XMLHttpRequest.open
The 'async' argument defaults to 'true' rather than 'false'.

HTMLElement.scrollIntoView
The 'top' argument defaults to 'true' rather than 'false'.

HTMLCanvasElement.toDataURL
In gecko this actually follows the rule given above since both empty
string and no argument is treated as "image/png". It's unclear if this
is correct according to spec.


Then there are two other odd ones out:

HTMLOptionsCollection.add
In gecko we here default the 'before' argument to '-1' rather than 0.
Though that's only because we don't yet implement the overload that
allows the 'before' argument be an object.

CanvasRenderingContext2D.drawImage
These functions (there are several overloads) behave in all sorts of
manners of ways depending on how many optional arguments are provided.
I don't think any default value here could be used.


So in the web platform today (at least the part that gecko
implements), only in 2 instances would we be helped by idl being able
to express default values for optional arguments. Possibly 3 if what
we're doing in HTMLCanvasElement.toDataURL is wrong.

I would argue that it's a nicer design to generally let optional
arguments default according to simple rules.

/ Jonas

Received on Tuesday, 1 December 2009 01:47:42 UTC