Remix.run Logo
▲ melodyogonna a day ago

Many people are against operator overloading, imo my only issue with C++ operator overloading is that you're allowed to overload the the assignment operator. From what I can see this is the root of many issues

▲bunderbunder a day ago | parent | next [-]

I’ve also experienced some unnecessary shenanigans related to blurring the distinction between primitive operations, and stuff that happens dynamically at run time such as method calls.

For a somewhat different example of the same general idea, look at how Java handles primitives and their boxed counterparts. = on two ints always compares their value. = on two Integers compares their pointers. But it uses flyweight instances for values up to 256, so in Java, whether two Integers with the same numeric value are equal according to the = operator depends on what that value is.

Then throw autoboxing and generics into the mix, and it suddenly becomes very easy to do refactorings that work fine in the unit tests (which tend to use small numbers) but fails in confusing ways in production.

This isn’t an example of operator overloading, of course. But I hope it more simply illustrates why, at least in certain problem domains, there’s value in being able to visually tell when surprising runtime shenanigans are and are not possible. And also to make it impossible to cross that boundary without touching all code that falls within the blast radius of that change.

▲wpollock a day ago | parent [-]

> But it uses flyweight instances for values up to 256, so in Java, whether two Integers with the same numeric value are equal according to the = operator depends on what that value is. ...

I believe Java caches Integer objects for values -128..+128, not 256. But that may be an implementation detail, and not something to count on.

▲bunderbunder a day ago | parent | next [-]

No, you’re right. I misremembered.

▲wpollock a day ago | parent [-]

> No, you’re right. I misremembered.

We both misremembered. I found where this is documented and the range is -128..127 inclusive: <https://docs.oracle.com/en/java/javase/27/docs/api/java.base...>

▲gf000 a day ago | parent | prev [-]

And since java 28's value types, == will work identically for all Integers, finally.

▲wpollock a day ago | parent [-]

Note that value types will still be a "preview" feature in Java 28.

I'm looking forward to structured concurrency in Java 28. I already thought of a title for my article "Java Concurrency - Past, Present, and Futures".

▲pjmlp 13 hours ago | parent | prev | next [-]

In functional programming languages basically any unicode character can be used as function name, those with overloading issues in C++ better never come close to FP.

▲vnorilo a day ago | parent | prev [-]

overloading the comma is also a special flavor. everyone should try it once.