5 ms·
> An operator can be overloaded in C plus plus, but right now operators in the Java pro- gramming language can not be overloaded by the programmer, though names
by senkora 1y ago
> An operator can be overloaded in C plus plus, but right now operators in the Java pro-
gramming language can not be overloaded by the programmer, though names of methods
may be overloaded. I would like to change that
It’s a shame that he lost this battle. Operator overloading really does make it a lot nicer to design certain kinds of libraries.
- chubot 1y agoFWIW another thing that Guy Steele advocated was value types, which as I understand it is ongoing work: https://en.wikipedia.org/wiki/Project_Valhalla_(Java_language) https://en.wikipedia.org/wiki/Project_Valhalla_(Java_languag... https://openjdk.org/projects/valhalla/ https://openjdk.org/projects/valhalla/ I have said in the past, and will say now, that I think it would be a good thing for the Java programming language to add generic types and to let the user define overloaded operators. He did get the generic types though, I think that was well over a decade ago now
- roetlich 1y agoOr you could use C# and get all of those features.
- steveklabnik 1y ago> I think that was well over a decade ago now This was in Java 5, in 2004. So two decades!
- swyx 1y agoi mean okay but also it makes it impossible to have an open source ecosystem with shared libraries because suddenly things dont mean the same thing in one system vs another. i was interviewing at Jane St and realized their OCaml completely doesnt work with the rest of the world - and its fine if you're Jane St i guess but that really sucks for the "health" of a language ecosystem.
- Someone 1y agoWhy would a + b suffer more from this than plus(a,b) ? In both cases, libraries will only clash if both define a function with the same name taking an A and a B. The only difference is that it is called + in the former case, and plus in the latter.
- mojifwisi 1y agoIt's syntactically more straightforward to resolve the issue of clashing definitions through namespacing with functions compared to operators. The following is pretty standard: foo::plus(a, b) // or foo.plus(a, b) bar::plus(a, b) // or bar.plus(a, b) Whereas this is more awkward: a foo::+ b // or a foo.+ b a bar::+ b // or a bar.+ b
- Someone 1y agoI would think a rule “a module that defines an operator overload must introduce at least one of the types involved” would prevent that problem, and still allow most, if not all, good uses of operator overloading.
- owlstuffing 1y agoAgree 1000% There’s a Java compiler plugin[1] that muscles in operator overloading pretty comprehensively, and works with all LTS JDKs. 1. https://github.com/manifold-systems/manifold/tree/master/manifold-deps-parent/manifold-ext#operator-overloading https://github.com/manifold-systems/manifold/tree/master/man...