Optional argument types
Dmitry Soshnikov
dmitry.soshnikov at gmail.com
Mon Sep 24 10:40:20 PDT 2012
Hi,
Just a curiosity -- what is the current state of the optional argument types proposal (if there is one)? Isn't it too late to propose?
Just recently participated in a discussion when these optional types are wanted to be used. Having a build-tool with pre-processing it's not that hard to add them manually to our sources, of course e.g.
function foo(/*string*/ bar, /*number*/ baz) { ... }
We could even add a small language extension having such preprocessors -- instead of comments use real keywords/constructor names:
function foo(String bar, Number baz) { ... }
If these are constructor names, then even new keywords are not needed, and the code just translates into:
function foo(bar, baz) {
if (typeof bar != 'string') throw TypeError(...);
if (typeof baz != 'number') throw TypeError(...);
}
That's it, literally it does only this and nothing else.
(Instead of `typeof` can be used [[NativeBrand]] check of course)
The same can go to the return values.
This is _only_ for arguments, not simple variables may "carry type" (since these are the values carry types, not the vars), just simple runtime type checking in the prologue of a function.
(I had have proposed similar before using JavaDocs instead https://gist.github.com/1186853, however, simple optional types could be better).
Does JS need this much?
Dmitry
More information about the es-discuss
mailing list