4 ms·
I wish it weren't referred to as 'operator overloading'. I don't want to think of them as 'operators' and I don't want to 'overload' anything. Is putting a me
by mrkeen 6y ago
I wish it weren't referred to as 'operator overloading'. I don't want to think of them as 'operators' and I don't want to 'overload' anything.
Is putting a method on a class called 'method overloading'?
I want to be able to define `plus` on my datatypes and I want to be able to define `+` on my datatypes.
`+` on my datatype overloads `+` on `Integer` no more than `get()` on my datatype overloads `get()` on someone else's datatype.
I am not taking an existing Integer type and giving it a new definition of `+`, which is what 'operator overloading' would be.
- Kototama 6y agoAccording to wikipedia it's also sometimes called operator ad hoc polymorphism. Better?
- clhodapp 6y agoIt's not any better. Ad hoc polymorphism is a defined thing just as overloading is a defined thing (in fact, it's a superset of OO-style "overloading"). When you allow operators to be defined for user-authored datatypes, these are features that you might choose to support for operators, but that doesn't mean that all user-defined operators are inherently using these features. Concretely, in defining a + operator on your BigInt class that can only accept another BigInt, you haven't overloaded the + operator. It would only be overloading if you defined a second form that accepted some other type of object.
- joshuamorton 6y agoSo if I want 3 + myBigInt to work? (Which I'll note most languages that support operator overloading support)
- clhodapp 6y agoYes, that would be actual operator overloading. Personally, I've always wanted languages to be stricter about that kind of thing. It's not that hard to explicitly express the desired conversions to make the types on both sides of your binary math operators the same and it feels a lot more "controlled" to me to do so.
- tlb 6y agoJulia is like this. a+b is just syntactic sugar for (:call :+ a b). There’s a function + for various type pairs. The fact that the compiler can optimize it to 1 instruction is an implementation detail.
- AnimalMuppet 6y agoBut when you overload '+', it overloads it as an operator, keeping things like "operator precedence". Overloading functions doesn't do that.
- rzwitserloot 6y ago> Is putting a method on a class called 'method overloading'? It most certainly is.. _if_ the method you are adding has the name of a functionality that is either inherently part of its spec or effectively so if by community conventions. e.g. when you make a method with signature `boolean equals(Object other)` in java, it is literally, as in the very spec itself calls it this, 'overloading'. The term is appropriate. `*` is commonly understood to be some sort of numeric operation with the properties that it is reflective, commutative, etc. If you decide to add a definition for a class you're writing, the term 'overloading' is entirely appropriate.
- pansa2 6y ago> I want to be able to define `plus` on my datatypes and I want to be able to define `+` on my datatypes. The problem with this approach is that, just as defining `plus(int)` on a class `C` allows you to call `c.plus(1)`, defining `+(int)` allows `c + 1` but not `1 + c`. Whereas the approach of adding overloads to a global `+` operator can support both `c + 1` and `1 + c`, by allowing you to define `+(C, int)` and `+(int, C)`.
- mcherm 6y ago> I want to be able to define `plus` on my datatypes and I want to be able to define `+` on my datatypes. So, I take it we agree that expressing the quadratic formula for BigDecimals as "x.pow(2).multiply(2).plus(x.multiply(3)).minus(5)" is awkward. Do you think that "x.^(2).(2).+(x.(3)).-(5)" is any better? Because if you were hoping to support "x^2 * 2 + (x * 3) - 5" then you'll need more than just allowing non-alphabetic characters in names... you will ALSO need special support for infix notation (which Java lacks today).