Open cchudant opened 7 years ago
Adding new operators: Impossible; lombok does not get a go until after the source has been syntactically parsed, and '===' isn't legal java.
Adding operator overloading as a concept: No. Both Roel and I think that the programming community at large has conclusively proved they are not grown up enough to be trusted with that power. Not happening.
Adding operator overloading for BI/BD specifically: Sure. That sounds awesome. If someone sends us a patch request that adds this, we'd accept it.
indexing overloading for maps and lists: No. How often have you called list.set() in your code? How about list.get()? If the answer is 'like once a year', you're doing it right. Not worth the mental overload. map.get() is used often enough, but, you should really be using .getOrDefault instead.
Get on map and also put is pretty common, right?
I suspect that most uses of put
are actually to initialize a collection; eventually, collection literals should put a stop to that. (Many implementations of the collections API already offer helper methods for this sort of thing.)
I'm curious about a special case of operator overloading, namely compareTo
, which is interesting because (1) it is defined by an interface, Comparator<T>
, which allows types to opt-in to defining their own unique natural ordering; (2) direct calls to compareTo
are much harder to interpret/write correctly than the theoretical overloaded equivalent using inequality operators; (3) unlike equals
, inequality operator overloading can be implemented in terms of existing Java operators (whereas equals
would require something like ===
as ==
is reserved for reference equality).
Maybe only add certain operators for different classes. E.g. for Classes which extend Number only +,-,/,== (== would also be OK in this case) etc. and for Collection only [] So you could at least prevent the "misuse" of OO a bit.
Btw: Did you ever try to write an emulator, implement a Matrix/Complex or Unsigned Number Class in Java? You will see that OO would be a great feature, so you can implement such stuff with more ease.
This is a Implementation of OO https://github.com/amelentev/java-oo
I bet you can create operator overloading in java. Have you ever thought of this feature? This would be AWESOME!
How it world work: (Not only for BigIntegers, but for all object)
Compare:
OO:
Vanilla java:
Arithmetic:
OO:
Vanilla java:
(Add could also be used for StringBuilder concatenation? or use += ?)
Binary (and ~):
OO:
Vanilla java:
Index:
OO:
Vanilla java:
Assignment:
OO:
Vanilla java:
Don't override the operator
==
and!=
because it will break things.I don't know if it's possible for you to add operators, but if you can, can you add operator === and !==? It would work like this: OO:
Vanilla java: