Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does not Closure type-check the parameters when using function.apply?

See below

/**
 * @param {string} a
 * @param {string} b
 */
var f = function(a, b){
    // ...
}

/**
 * @param {string} a
 * @param {boolean} c
 */
var h = function(a, c){
    f.apply(this, arguments); // no compile error
    f.apply(this, [a, c]);    // no compile error
    f.call(this, a, c);       // compile error: does not match formal parameter
}

Why does Closure raise an error only when using call and not apply?
Is there a way I can made closure type-check the parameters even when I'm using apply?

like image 431
oldergod Avatar asked Jan 14 '15 00:01

oldergod


1 Answers

Because (a) the type checker doesn't yet have the concept of an tuple type and (b) it is rare to call a method with an array literal. When using .call determining which argument is assigned to which parameter slot is trivial.

If the type system grows a tuple type, it would make sense to put more effort into checking .apply as the array "slot" types and length is more likely to be known.

like image 60
John Avatar answered Sep 30 '22 16:09

John