New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Method Overloading #73
Comments
Well, first there's the obvious complications when:
These issues can be substantially avoided by placing clever restrictions on the declaration of overloaded methods (essentially requiring that the parameter types have empty meets, and using the upper bound of a type parameter as its "type", etc, etc). But then there were two features of our type system that screwed this up. First:
Now, I suppose you could handle this by an error on the client side, and anyway we're starting to have some second thoughts about introductions. Finally:
This is the real major problem. Consider:
does |
I guess you could say that we don't support overloaded methods for exactly the same reason that we don't support overloaded attributes. |
I'm guessing the problem with this is the difference between static and dynamic typing. Java uses the static type of the argument to determine whether to call
Here I'm guessing the problem is ambiguity:
Optional parameters, as well as variatic methods, basically define method signatures at once, causing more opportunity for ambiguity. So these three examples all deal with ambiguity of an invocation. Your last example has to do with ambiguity of a reference without invocation. This last problem is strictly more difficult than the first, and it doesn't seem like you have solutions to it for optional parameters, variatic methods, or generic methods. I'm gonna leave this one 'til later, cuz solving the first one solves another problem I'm worried about: operator overloading. (P.S. Operator overloading is simply the idea that an operator can be defined for multiple types; it is not the ability to define new operator symbols.) Every now and then I ask people for feature requests in whatever languages they deal with. One reasonable request I get a lot is better handling of vectors and matrices. At the least, people want to be able to use standard operators so that they can write It seems your goal is to have a definition for The best solution I've been able to come up with is to explicitly add disjointness to the language. Allow users to label classes and interfaces (such as Then clearly the next step is to only allow method and operator overloadings when the parameter types are disjoint from each other. This also allows the return types of the overloadings to depend more sophisticatedly on the parameter types, and gets rid of that There are more issues with overloading, but this is one important step so I wanted to see what y'all thought before moving on to other steps. |
Yeah. I decided not to open up the whole issue of multimethods, simply because they just don't map nicely to Java bytecode, as far as I can see.
Exactly, it's really the problem of method references that convinced me to drop overloading. I don't want to have language features that simply don't work nicely together.
Sure, but we don't even allow you to define
Yeah, this one frankly bothers me. I would like to be able to do
I don't think that actually breaks anything. A perfectly reasonable implementation of
No, beyond that, I'm of the view that if you're using multiple libraries that all define their own special meaning of
OK, so this would be a nice way to be able to do what I described above as "essentially requiring that the parameter types have empty meets" but for interfaces as well as classes. I don't believe it to be necessary for classes, since you can always decide if they are disjoint or not by looking up the superclass hierarchy. |
Do you mean
But it also improves the readability in many situations. My guess is more good programmers would appreciate having the syntax than would be discouraged by the bad programmers who misuse it. I'm not suggesting opening this up to all operators, probably just Regardless, if you want it to be possible to do this for
I don't like the idea that I could strengthen the inheritance hierarchy in my library and by doing so unknowingly break someone else's code that mistakenly assumed two currently disjoint classes would forever be disjoint. By requiring |
yes, sorry.
If you can trust the other developers in the ecosystem to not use
I want people to be able to add
Show me the specific example you're thinking of. |
I have to add that I agree with Gavin when he said that we don't allow method overloading for the same reasons we don't allow attribute overloading. Ad absurdum we could say that the type checker could resolve the proper attribute |
A data point here is that on Saturday I spoke to some folks who seemed pretty bothered by the fact that we don't have overloading, and then when I demoed that I can write:
and
they were a lot happier. These are, after all, the two major usecases for overloading in Java. |
Closing this issue, since the absence of overloading has become a distinctive feature of the language. Doesn't look like we're ever going to introduce it, except, perhaps, someday, in an impoverished form where it's basically a syntax sugar, i.e. some day we could support this:
Since we could transform this code into:
But that would really be a different language feature, not "overloading" as such. |
So before I start discussing casting in #65, I wanted to know your reasons for not having method overloading, since casting is your primary workaround. I can think of a few, but I don't know which ones overlap with your reasons and which ones you wouldn't care about, which makes it hard for me to figure out solutions.
P.S. I'll probably play devil's advocate a bit so I can get a better sense of what y'all want.
The text was updated successfully, but these errors were encountered: