Re: Reconciling handling of optional arguments and handling of default values across ES and webidl

On 5/9/13 11:11 AM, Andreas Rossberg wrote:
> I think existing JS legacy notwithstanding it is generally bad
> practice for an API to make an observable distinction between passing
> undefined and not passing anything, especially in the light of ES6's
> treatment of defaults. So I don't think a higher-level declarative
> mechanism like WebIDL should ever want to support that.

That was sort of my feeling too; just trying to figure out what the 
right spec language here is.

Here's an interesting concrete example: 
CanvasRenderingContext2D.drawImage, which is specified like so at the 
moment:

   void drawImage(CanvasImageSource image, unrestricted double dx,
                  unrestricted double dy);
   void drawImage(CanvasImageSource image, unrestricted double dx,
                  unrestricted double dy, unrestricted double dw,
                  unrestricted double dh);
   void drawImage(CanvasImageSource image, unrestricted double sx,
                  unrestricted double sy, unrestricted double sw,
                  unrestricted double sh, unrestricted double dx,
                  unrestricted double dy, unrestricted double dw,
                  unrestricted double dh);

In JS-speak, the behavior it's generally aiming for would probably be 
expressed like this today:

   function drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh) {
     if (arguments.length != 3 && arguments.length != 5 &&
         arguments.length != 9) {
       throw new TypeError();
     }

     if (arguments.length != 9) {
       dx = sx; sx = 0;
       dy = sy; sy = 0;
     }

     if (arguments.length == 5) {
       dw = sw; sw = undefined;
       dh = sh; sh = undefined;
     }

     // Proceed with the algorithm, treating undefined sw/sh/dw/dh as
     // meaning intrinsic sizes for the image, but being careful to
     // check arguments.length some more to see whether the undefined
     // means that or NaN, or something.
   }

So this is a quite crappy API from the point of view of undefined == not 
passed because the version that passes the optional source rect puts it 
_before_ the destination rect.  And it's not quite clear what this:

   drawImage(image, 0, 0, undefined);

should do.  In today's world it throws.  Should WebIDL preserve the 
ability to do that in that situation?

-Boris

Received on Thursday, 9 May 2013 15:27:56 UTC