Annotation Feature Wishes (Type Templates & Functions with Properties)

54 views
Skip to first unread message

Bernd Wessels

unread,
May 30, 2012, 12:38:40 AM5/30/12
to closure-lib...@googlegroups.com
Hi guys, here are two feature wishes regarding the Google Closure Library Annotation System.


1. Functions with properties

function iReturnAFunctionWithAdditionalProperties(a) {
  var x = a;
  var f = function(opt_b) {
    if(arguments.length == 0) return x;
    else x = opt_b;
  };
  f.hello = function(text){alert(text); return x;};
};

/**
 * @type {function(number=)}    Will not work in this case.
 */
var myF = iReturnAFunctionWithAdditionalProperties(4711);

myF(815);                  Valid JavaScript and works absolutely fine,
var test = myF();         BUT at the moment there is no way to
myF.hello('ola');          annotate this in a way that the closure compiler is happy.


I would suggest something like this to be added to the Closure Annotation:

/**
 * @param {number=} opt_value
 * @function
 */
MyLib.FunctionWithAdditionalProperties = function(opt_value) {};

/**
 * @param {string} text
 * @returns {number}
 */
MyLib.FunctionWithAdditionalProperties.prototype.hello(text) {};


In our example that would work like this:

/**
 * @type { MyLib.FunctionWithAdditionalProperties }
 */
var myF = iReturnAFunctionWithAdditionalProperties(4711);

myF(815);                  wow, great
var test = myF();         perfect
myF.hello();                wonderful



2. Type Templates

It is really hard to describe, but I would love some kind of type templates in the Google Closure Annotation System.

Here is an example:

/**
 * @param {template(0)} a
 * @param {template{1)} b
 * @param {string} c
 * @returns {template(1)}
 * @function
 */
MyLib.Something = function(a, b, c) { return b; };

/**
 * @type {MyLib.Something}
 * @template(0) {number}
 * @template(1) {string}
 */
var myF1 = MyLib.Something;

/**
 * @type {MyLib.Something}
 * @template(0) {Object}
 * @template(1) {number}
 */
var myF2 = MyLib.Something;

/**
 * @type {string}
 */
var test1 = myF1(17, 'seventeen', 'hello');

/**
 * @type {number}
 */
var test2 = myF2({grunz: 'ui'}, 48, 'hello');


Maybe somebody of the official Google Closure guys can tell me if that makes sense and if this here is the right place for Feature Wishes.

Best regards
Bernd
Reply all
Reply to author
Forward
0 new messages