2 ms·
That sort of explicit type annotation approach falls over for the more complex cases, like the function signature one I described. For example, let's say you ha
by kevingadd 14y ago
That sort of explicit type annotation approach falls over for the more complex cases, like the function signature one I described. For example, let's say you have an object called a StringBuilder, like the one in Java/C#, and it's got a method for appending text to it. In JS, you'd use it like this:
var bananaCount = 5;
var sb = new StringBuilder();
sb.append("There are ");
sb.append(bananaCount);
sb.append(" bananas");
If you merely add type annotations, you're not going to win much here:
/* v8-type: this(StringBuilder) (any) -> none */
sb.append = function (...) { ... };
If you go to the trouble of adding function overloading, maybe things get a little better - TypeScript does something like this hypothetical:
sb.append = {
/* v8-type: this(StringBuilder) (string) -> none */
function (...) { ... },
/* v8-type: this(StringBuilder) (number) -> none */
function (...) { ... }
}
OK, but now what if you've got collection types? If you create an instance of List, what type is List.items? It'd have to be any[], unless you also add something like generics. So now you have to add a generics system or you have to introduce type casts, just to avoid having all your type information polluted by 'any's.
This may still be kind of unclear; it is often hard to see the failings of a type system until you see it in action on a larger application. In practice, it comes down to this: The type inferences that an engine like V8 or SpiderMonkey can draw at runtime end up being extremely accurate, and provide a lot more information than a compiler for a language like C or C# or Erlang has access to.