This project is read-only.

Overload syntax

Topics: General, Language Specification
Feb 5, 2013 at 6:39 PM
Edited Feb 5, 2013 at 6:40 PM
Right now, specifying an overload requires you to duplicate the function signature with different parameters and no method body. I think this is fine for overloads with very different signatures, but for more "simple" overloads, it seems like overkill.

Often I find I just need to specify that a variable can be of one or more types (but the name and order in the parameter list doesn't change). For simple cases, I think the following syntax would be a lot more concise:
function foo(a: string|number, b: Date|string) { ... }
This allows you to specify 4 overloads without having to copy and paste function signatures. I think this combined with optional parameters could cover many JavaScript "overloads" in just a single concise line.

What do you think? Does anyone see any problems with this approach?
Feb 6, 2013 at 3:05 PM
Shorthand to give you all the permutations of overloads might be handy, though I think the confusion might come because "string|number" isn't actually a type.

For example, we probably won't end up also supporting:
var x : string|number;
Because of the complexity it adds to the typechecking algorithm.
Feb 6, 2013 at 3:54 PM
I agree there is no need to support
var x: string|number;
as that somewhat defeats the purpose of strong typing to begin with.

I think in the case of overloads, the simplest solution would be for the compiler to treat a parameter marked with multiple types as an any. This is basically what you have to do now anyway when you have overloads with different types for the same parameter. The programmer would then have to do a type check to figure out what the type actually is, just like they do currently.