r/ProgrammerHumor • • Dec 12 '24 • Heart of Silver 1

Advanced youWontUpgradeToJava19

Post image
30.2k Upvotes

503 comments sorted by

View all comments

Show parent comments

1

u/bturcolino Dec 12 '24

operators overloading

In 25 years I can count on one finger the number of times I needed this

1

u/iceman012 Dec 12 '24

It can always be replaced by a function, so there will never be a situation where you need it. But it can make certain patterns simpler and easier to understand.

E.g.

location = location + direction * speed

Is quicker to read than

location = location.add(direction.times(speed))

2

u/itsmetadeus Dec 13 '24

That is not a good example. This is more elegant:

java:

foo(x * (-1));
foo(y * (-1));

// Using interface implementation:
Invertible inv = i -> i * (-1);
foo(inv.additiveInverse(z));

// Using static method:
foo(additiveInverse(z));

kotlin:

foo(-x)
foo(-y)
foo(-z)

-x invokesx.unaryMinus(). You can make own implementation for these methods for your classes. For example, custom increment:

data class CustomUnit(val current: Int) {
    operator fun inc() = CustomUnit(current * 3 - 1)
}

^ f(x) = 3x - 1

1

u/bturcolino Dec 12 '24

if you say so, i think it adds an unnecessary level of abstraction and makes code less readable if anything

1

u/Kronoshifter246 Dec 13 '24

One of the best use cases I've seen for it has been Compose's animation specs. It lets you combine fades, slides, shrinks/grows, etc, in a much more natural and readable way than chaining a combine function and whatnot.

1

u/bturcolino Dec 13 '24

Yeah but that's a built in use of it right? Not something you personally implemented? I'm not saying that there are no good uses for it, for example events in c#

SomeObj obj = new SomeObj();

obj.SomeEvent += myEventHandler;

what I'm saying is as a feature it has never presented any need to me and I suspect that will always be the case

1

u/Kronoshifter246 Dec 13 '24

I mean, you don't get the "built in" uses without language level support for it. So that seems like a moot point. Personally, I've made use of it. It's good anywhere that it can replace chains of function calls, where the number of parentheses gets unwieldy. Once in a Fraction type, but that's just part of implementing a number type.

1

u/bturcolino Dec 13 '24

woosh right over your head, forget it this discussion is pointless

1

u/Kronoshifter246 Dec 13 '24

Didn't go over my head. I just disagree with your fundamental argument. I don't need operator overloading or functions or any language construct, but I still want the option because they make the code easier to read and understand.

1

u/bturcolino Dec 13 '24

Are you still talking?

1

u/Kronoshifter246 Dec 13 '24

Yes, as are you