javascript - Google closure: trouble type checking parameters that should be functions - Stack Overflow

admin2025-04-08  2

I'm messing around with the type checking in google's closure piler. The type system seems useful, if perhaps not the most sophisticated out there. I'm happy with most of the limitations, but this one just seems a bit weird.

I'm seeing problems giving type annotations for functions passed as arguments. In particular, if the type of the passed function is itself not fixed. So for example, I'd like to write code similar to this:

/**
 * @param {Array} xs
 * @param {function(*) : boolean} f
 * @return {Array}
 */
var filter = function (xs, f) {
    var i, result = [];
    for (i = 0; i < xs.length; i += 1) {
        if (f(xs[i])) {
            result.push(v);
        }
    }
    return result;
};

filter([1,2,3], function (x) { return x > 1; });

Passing "--js_error checkTypes" to the piler, I get this:

test.js:17: ERROR - left side of numeric parison
found   : *
required: number
    filter([1,2,3], function (x) { return x > 1; });
                                          ^

So, what's wrong? Can I specify that a parameter ought to a function with one argument, without specifying the type of that argument? Am I doing something wrong, or is this just a limitation of the type checker?


Chad suggests annotating the anonymous function passed to filter to help the type-inference out a bit:

filter([1,2,3], function (x) { return /** @type {number} */ (x) > 1; });

That works ok for filter(), but it seems a little unsatisfying (why does the piler need that annotation?), and doesn't work for more plex cases. For example:

/**
* @param {Array|string} as
* @param {Array|string} bs
* @param {function(*, *): *} f
* @return {Array}
*/
var crossF = function (as, bs, f) {};

/**
* @param {Array|string} as
* @param {Array|string} bs
* @return {Array}
*/
var cross = function (as, bs) {};

var unitlist = crossF(['AB', 'CD'], ['12', '34'], cross);

It seems like the type of everything here should be apparent to the piler. And in fact it plains directly about matching the type of the function parameter:

test.js:52: ERROR - actual parameter 3 of crossF does not match formal parameter
found   : function ((Array|null|string), (Array|null|string)): (Array|null)
required: function (*, *): *
var unitlist = crossF(['ABC', 'DEF', 'GHI'], ['123', '456', '789'], cross);

Accepted answer below addresses this case.

I'm messing around with the type checking in google's closure piler. The type system seems useful, if perhaps not the most sophisticated out there. I'm happy with most of the limitations, but this one just seems a bit weird.

I'm seeing problems giving type annotations for functions passed as arguments. In particular, if the type of the passed function is itself not fixed. So for example, I'd like to write code similar to this:

/**
 * @param {Array} xs
 * @param {function(*) : boolean} f
 * @return {Array}
 */
var filter = function (xs, f) {
    var i, result = [];
    for (i = 0; i < xs.length; i += 1) {
        if (f(xs[i])) {
            result.push(v);
        }
    }
    return result;
};

filter([1,2,3], function (x) { return x > 1; });

Passing "--js_error checkTypes" to the piler, I get this:

test.js:17: ERROR - left side of numeric parison
found   : *
required: number
    filter([1,2,3], function (x) { return x > 1; });
                                          ^

So, what's wrong? Can I specify that a parameter ought to a function with one argument, without specifying the type of that argument? Am I doing something wrong, or is this just a limitation of the type checker?


Chad suggests annotating the anonymous function passed to filter to help the type-inference out a bit:

filter([1,2,3], function (x) { return /** @type {number} */ (x) > 1; });

That works ok for filter(), but it seems a little unsatisfying (why does the piler need that annotation?), and doesn't work for more plex cases. For example:

/**
* @param {Array|string} as
* @param {Array|string} bs
* @param {function(*, *): *} f
* @return {Array}
*/
var crossF = function (as, bs, f) {};

/**
* @param {Array|string} as
* @param {Array|string} bs
* @return {Array}
*/
var cross = function (as, bs) {};

var unitlist = crossF(['AB', 'CD'], ['12', '34'], cross);

It seems like the type of everything here should be apparent to the piler. And in fact it plains directly about matching the type of the function parameter:

test.js:52: ERROR - actual parameter 3 of crossF does not match formal parameter
found   : function ((Array|null|string), (Array|null|string)): (Array|null)
required: function (*, *): *
var unitlist = crossF(['ABC', 'DEF', 'GHI'], ['123', '456', '789'], cross);

Accepted answer below addresses this case.

Share Improve this question edited Apr 21, 2012 at 7:41 Ned asked Apr 12, 2012 at 20:15 NedNed 2,27218 silver badges24 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Change the declaration of the filter from "*" (everything) to "?" (unknown). The piler only checks known types. So when the piler tries to infer the function signature for the function expression at the call site, it resolves the parameter "x" to "?" (an unknown type) (which can be used as anything), instead of "*" (every possible type) which often needs to be restricted before use:

/**
 * @param {Array} xs
 * @param {function(?) : boolean} f
 * @return {Array}
 */
var filter = function (xs, f) {
    var i, result = [];
    for (i = 0; i < xs.length; i += 1) {
        if (f(xs[i])) {
            result.push(v);
        }
    }
    return result;
};

When there are no annotations on a function, the piler assumes that it can take a variable number of arguments of any type and return any type. For this reason, many of the extern functions are annotated like this:

/** @return {undefined} */
function MyFunction() {}

This way they will properly type check.

For your case, the easiest solution is to type cast the argument to a number inside the function (note the extra parenthesis which are required):

filter([1,2,3], function (x) { return /** @type {number} */ (x) > 1; });

One mon approach is to use the type annotation {!Function}, which accepts any function object.

The issue with the ALL type (*) has been reported here: Issue 708

This looks like a bug to me. You should file it here: http://code.google./p/closure-piler/issues/list

If you don't specify the types, it should be "unknown"(?) not "any"(*). The piler doesn't (or shouldn't) type check the use of unknown types.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744127487a232530.html

最新回复(0)