W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2009

Re: Handling too few arguments in method calls

From: Jonas Sicking <jonas@sicking.cc>
Date: Mon, 22 Jun 2009 17:29:47 -0700
Message-ID: <63df84f0906221729h6b0b1fd5n427edf76fcc14831@mail.gmail.com>
To: public-webapps@w3.org
On Sun, Jun 21, 2009 at 5:53 PM, Cameron McCormack<cam@mcc.id.au> wrote:
> From some very brief testing, it seems that Firefox and Opera tend
> to throw an exception when calling a method with too few arguments,
> while IE, Safari and Chrome will assume that the missing arguments were
> the undefined value.  Extra arguments tend to be ignored.
>
> Does anyone have an opinion on which way this should be specified in Web
> IDL?  Assuming this interface:
>
>  interface A {
>    /* f1 */ void f(in DOMString a);
>    /* f2 */ void f(in DOMString a, in float b);
>    /* f3 */ void f(in DOMString a, in float b, in float c, in float d);
>  };
>
> option 1 would be to have:
>
>  a.f() be like calling f1 with (undefined)
>  a.f('a') be like calling f1 with ('a')
>  a.f('a', 1) be like calling f2 with ('a', 1)
>  a.f('a', 2, 3) be like calling f3 with ('a', 2, 3, undefined)
>  a.f('a', 2, 3, 4, 5) be like calling f3 with ('a', 2, 3, 4)
>
> and option 2 would be to have:
>
>  a.f() throw an exception
>  a.f('a') be like calling f1 with ('a')
>  a.f('a', 1) be like calling f2 with ('a', 1)
>  a.f('a', 2, 3) throw an exception
>  a.f('a', 2, 3, 4, 5) be like calling f3 with ('a', 2, 3, 4)
>
> Web IDL currently says to throw on any incorrect number of arguments,
> so it seems that it should change to be one of the above two options.

If we went with option 1, what is the effect of the [optional] flag?
I.e. what is the difference between 1 and putting [optional] on all
arguments?

I'd prefer to go with option 2, but use [optional] in more specs. This
way we can in places where it really does not make sense to leave out
an argument (such as for Node.appendChild) make that throw, while in
cases where leaving arguments out can be dealt with using [optional].

While it's true that in javascript calling a function with too few
arguments doesn't throw, however a proper implementation will then
check any required attributes and manually throw if they are missing
(or simply trying to access properties on a passed in argument will
throw if the argument is undefined).

By using IDL to mark up which arguments are required, we don't have to
do that using prose in the specification.

An alternative way would be to go with option 2, but introduce a
[required] keyword.

/ Jonas
Received on Tuesday, 23 June 2009 00:30:43 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:31 GMT