Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active August 27, 2015 14:30
Show Gist options
  • Save ochafik/d8f701557f5bcab68940 to your computer and use it in GitHub Desktop.
Save ochafik/d8f701557f5bcab68940 to your computer and use it in GitHub Desktop.
Experiment on Closure Compiler's function types (behaviour of all '*' vs. unknown '?')
// Run with:
// java -jar compiler.jar --language_in=ECMASCRIPT6_STRICT -O ADVANCED --warning_level VERBOSE functions.js
/**
* @param {function(*)} f1 ERROR
* @param {function(*):*} f2 ERROR
* @param {function(?):*} f3
* @param {function(*):?} f4 ERROR
* @param {function(?):?} f5
* @param {function()} f6 ERROR
* @param {Function} f7
* @param {function(number):number} f8
*/
function higherOrder(f1, f2, f3, f4, f5, f6, f7, f8) {
console.log(f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8);
}
/** @return {number} @param {number} x */
function f(x) { return x + 1; };
higherOrder(/* ERROR */ f, /* ERROR */ f, f, /* ERROR */ f, f, /* ERROR */ f, f, f);
/**
* @param {function({x: (*|undefined)})} f1
* @param {function({x: (?|undefined)})} f2
*/
function higherOrderNamed(f1, f2) {
console.log(f1 + f2);
}
/** @param {{x: (*|undefined)}} opts */
function fany(opts) { return opts.x; };
/** @param {{x: (number|undefined)}} opts */
function fnumber(opts) { return opts.x; };
higherOrderNamed(fany, fany);
higherOrderNamed(/* ERROR */ fnumber, fnumber);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment