I was messing with type checking in google close compiler. A type system seems useful if it may not be the most complex. I am pleased with most of the limitations, but it just seems a little strange.
I see problems with type annotations for functions passed as arguments. In particular, if the type of the function passed is not itself fixed. For example, I would like to write code similar to this:
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 compiler, I get the following:
test.js:17: ERROR - left side of numeric comparison
found : *
required: number
filter([1,2,3], function (x) { return x > 1; });
^
So what happened? Can I indicate that a parameter should have a function with one argument, without specifying the type of this argument? Am I doing something wrong, or is it just a typing restriction?
, , :
filter([1,2,3], function (x) { return (x) > 1; });
(), ( ?) . :
var crossF = function (as, bs, f) {};
var cross = function (as, bs) {};
var unitlist = crossF(['AB', 'CD'], ['12', '34'], cross);
, . :
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);
.