Hello,
Is it possible to have some kind of function overloading with Closure
Compiler? Let's take this function as example:
/**
* @param {!Dimension|number} sizeOrWidth
* @param {number=} height
*/
function setSize(sizeOrWidth, height) {}
This function can be called in two ways (Actual decision how to
interpret the parameters is up to the implementation of course):
setSize(new Dimension(1024, 768));
setSize(1024, 768);
Unfortunately according to the type annotations it also allows illegal
use like this:
setSize(1024);
setSize(new Dimension(1024, 768), 512);
And it even gets worse when it comes to return types which depend on the
parameter types. Take a look at the jQuery externs and you can imagine
how many wrong API usages Closure Compiler can't detect.
It's clear that JavaScript doesn't support overloading but is it somehow
possible to give Closure Compiler more hints about how this function can
be used? If not, anyone here thinks this would be a cool feature? Maybe
it could be implemented like this:
/**
* @param {!Dimension} size
* @overload
*/
function setSize(size) {}
/**
* @param {number} width
* @param {number} height
* @overload
*/
function setSize(width, height) {}
/**
* Sets the element size.
*
* @param {!Dimension|number} sizeOrWidth
* @param {number=} height
* @overloaded
*/
function setSize(sizeOrWidth, height)
{
// Real implementation goes here.
}
So there are two dummy functions (Must be defined before the real one,
or in an externs file) annotated with "@overload" and the real function
annotated with "@overloaded". Compiler can check if the
@overload-functions have compatible signatures to the @overloaded
function. And function calls can be checked against all the @overload
functions (Must NOT check against the real @overloaded function!).
Documentation tools can ignore the @overloaded function and only list
the @overload functions.
--
Bye, K <
http://www.ailis.de/~k/>
[A735 47EC D87B 1F15 C1E9 53D3 AA03 6173 A723 E391]