projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.85k stars 2.38k forks source link

Operator overloading #1300

Open cchudant opened 7 years ago

cchudant commented 7 years ago

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:

BigInteger i1 = BigInteger.valueOf(5);
BigInteger i2 = BigInteger.valueOf(5);
boolean b1 = i1 < i2,
   b2 = i1 <= i2,
   b3 = i1 > i2,
   b4 = i1 >= i2;

Vanilla java:

BigInteger i1 = BigInteger.valueOf(5);
BigInteger i2 = BigInteger.valueOf(5);
boolean b1 = i1.compareTo(i2) < 0,
   b2 = i1.compareTo(i2) <= 0,
   b3 = i1.compareTo(i2) > 0,
   b4 = i1.compareTo(i2) >= 0;

Arithmetic:

OO:

BigInteger i1 = BigInteger.valueOf(5);
BigInteger i2 = BigInteger.valueOf(5);

BigInteger b1 = i1 + i2,
   b2 = i1 - i2,
   b3 = i1 * i2,
   b4 = i1 + i2,
   b5 = i1 % i2,
   b6 = -i1;

Vanilla java:

BigInteger i1 = BigInteger.valueOf(5);
BigInteger i2 = BigInteger.valueOf(5);

BigInteger b1 = i1.add(i2),
   b2 = i1.substract(i2),
   b3 = i1.multiply(i2),
   b4 = i1.divide(i2),
   b5 = i1.mod(i2),
   b6 = i1.negate();

(Add could also be used for StringBuilder concatenation? or use += ?)

Binary (and ~):

OO:

BigInteger i1 = BigInteger.valueOf(5);
BigInteger i2 = BigInteger.valueOf(5);

BigInteger b1 = i1 & i2,
   b2 = i1 | i2,
   b3 = i1 ^ i2,
   b4 = i1 << i2,
   b5 = i1 >> i2,
   b6 = ~i1;

Vanilla java:

BigInteger i1 = BigInteger.valueOf(5);
BigInteger i2 = BigInteger.valueOf(5);

BigInteger b1 = i1.and(i2),
   b2 = i1.or(i2),
   b3 = i1.xor(i2),
   b4 = i1.leftShift(i2),
   b5 = i1.rightShift(i2),
   b6 = i1.not();

Index:

OO:

Map<String, String> map = new HashMap<>();
List<String> list = new ArrayList<>();

map["hello"] = "hi";
String str = map["hello"];

list[0] = "Heya!"; //Same as arrays
String str2 = list[0];

Vanilla java:

Map<String, String> map = new HashMap<>();
List<String> list = new ArrayList<>();

map.put("hello", "hi");
String str = map.get("hello");

list.set(0, "Heya!");
String str2 = list.get(0);

Assignment:

OO:

BigInteger i = 0;

Vanilla java:

BigInteger i = BigInteger.valueOf(0);

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:

boolean bool = str === "Hello world!";

Vanilla java:

boolean bool = "Hello world!".equals(str);
rzwitserloot commented 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.

cchudant commented 7 years ago

Get on map and also put is pretty common, right?

rschmitt commented 7 years ago

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).

masl123 commented 7 years ago

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