for example it makes perfect sense to overload for example + on a vector class/structure so that you can do
a + b
instead of some shit like addvectors(a, b)
OH GOD I JUST WROTE SOMETHING THAT RESEMBLES JAVA CODE *kills self*
Name:
Anonymous2007-08-20 5:41 ID:caKSnTZI
>>1
Why not. Operators are just syntactic sugar for functions. As long as your language is powerful enough to let you use them like functions and overload them, and it's not shitty enough to perform automagic shitty type conversion that will always make everything fail, you should be alright.
There are cases where operators are the right thing to use. For example, type comparison. You always use the equality symbol, which is usually == in your programming language, to test for object equality, so == is a better name than equals or some other Java-like bullshit. It's always far more intuitive and easier to understand, not to mention shorter, to do a == b than a.equals(b) (UGH! THE JAVA SHIT!), and the infix syntax for == better reflects the fact == is a function of two operands and its operands are expected to be of equal/similar/compatible types.
Of course, don't make it Perl. The & operator shouldn't really hire a new employee or mop the floor, or eat the protagonist as in roguelike games. Just like you should name your functions properly, you should choose your operators properly, and if none fit, use a function.
Java designers are apparently all idiots. "Oh noes, programmers could choose to use the '+' operator in places where it's semantically impropriate, so let's force everyone to write myint1.add(myint2) instead. Yay, the improved readability of code by preserving semantics by removing them. Yay, making all user defined classes second class citizens. Yay! After work, let's all go post comments on Youtube."
Name:
Anonymous2007-08-20 5:54 ID:ZqwQ7CSs
>>7 impropriate
That word, I do not think it means what I think it means.
Name:
Anonymous2007-08-20 9:15 ID:k/xjY8U4
Good, provided you don't do something stupid like overload + without knowing what a ring is.
>>4
Yeah, this is the first example people ever mention. I've never heard of any others.
Even with vectors, you're left with the question as to what kind of product operator*() on them produces. Cross or dot product? Whut? Can't overload on return type you know, have to pick one and have a named method for the other... oh noes, java-style code! The horror.
Perhaps I'm just jaded. But I quite prefer to read code that means what it says, rather than having to deduce types yourself, types that may have been declared two screenfuls up, and then go to the appropriate spot in the appropriate doxygen (or what have you) autogenerated piece of shit manual. With C, or Java? You have a tags file for the source at hand. You move your cursor on top of the function call you're interested in and press the magic key(s) your editor wants. Abracadabra!
And don't get me started on Boost.
Name:
Anonymous2007-08-20 11:57 ID:CejhsYus
>>9
Wow finally all my overloading rants have sunk in!
Except maybe a field would be safer (j/k)
Name:
Anonymous2007-08-20 13:12 ID:KS1jlx4H
i leik it
(+) :: Num a => a -> a -> a
a + b = a - b
>>15,16
WAR IS PEACE
FREEDOM IS SLAVERY
ADDITION IS SUBSTRACTION
Name:
Anonymous2007-08-21 16:33 ID:p2mbYOw/
>>17 OKAY YOU FUQIN ANGERED AN EXPERT PROGRAMMER
GODFUCKIGNDAMN
FIRST OF ALL, YOU DON'T FUQIN KNOW WHAT A MAN PAGE IS
SECONDLY, THIS IS /prog/ DO NOT DEMAND USEFUL ANSWERS THE WAY YOU WANT THEM TO BE
THIRDLY PROGRAMMING IS ALL ABOUT PHILOSOPHY AND ``ABSTRACT BULLSHITE'' THAT YOU WILL NEVER COMPREHEND
AND FUQIN LASTLY, FUCK OFF WITH YOUR BULLSHYT
EVERYTHING HAS ALREADY BEEN ANSWERED IN>>3,4,10
>>20 (((ok bitch) (you asked for it) (here goes)) (im taking out the fucking (bold paranthesis on you)) (you fuqing angered an expert programmer) (ive been here for(expt 3 88888000)years longer than you) (ive read sicp twice) (i know every programming language in the world including apl) (if u wanna batl(lets do it)) (ill crush you like a bean))
Topic:
I'm ambivalent about operator overloading. I have always thought there should be serious consideration involved before one creates a local disambiguation of, most commonly, simple arithmetic operators. The extended "==" tends to be good, but that just goes into the second point: overloading an operator is essentially the same as writing a function/method, so why not just write a function/method? Neither design promotes readability of code as you still have to scroll through it to find the overloading or the method implementation at least once. And, in compiled code, it doesn't matter (I don't think so anyway; feel free to correct me on this).
On the other hand, operator overloading does promote heuristic readability. Programs that use vector math, or any kind of math, can look much more logical if the code looks closer to what you were taught in school. I can't count the number of times I exchanged 'pow' for '^', for example. If this is the case, however, then does my argument become "operator overloading is a nonessential feature preserved for the benefit of the programmer?"
I'd rather not the reason be that trivial if I can afford it.
I do not distinguish operators from functions because I am not a faggot.
Name:
Anonymous2009-11-13 13:05
>>34 operator overloading is a nonessential feature preserved for the benefit of the programmer
As is everything, really. You can choose to program in machine code, I'll invent some new operators in Haskell.
(~>) :: a -> (a -> b) -> b
infixl ~>
(~>) = flip ($)
data Person = Person {
address :: Address
, age :: Integer
} deriving (Show)
data Address = Address {
street :: String
, house :: Integer
} deriving (Show)
And now given p :: Person you can do p~>address~>street OOP IN HASKELL
I, too, do not distinguish operators from functions because I am not a faggot.
Name:
Anonymous2009-11-13 13:21
>>9
Point being? Even floating point addition is not commutative.
>>12
As with everything, you have to be mindful of the domain you're working in. Even in mathematics a product can mean any number of different operations as long as they follow a set of rules. C++ has roots in academics so it's not surprising the language allows for something akin to a Hilbert space definition.
>>36
Being able to access a record field through an operator does not OOP make.
Also, that isn't operator overloading, it's operator creation, and that distinction is relevant because one of the things people bitch about operator overloading is that unrelated meanings can be associated with existing operators (which , as a sidenote, Haskell's type class system makes extra hard to accomplish).
Name:
Anonymous2009-11-13 16:18
Guide to Function Naming in Haskell
1. Think about what the function does. 2. Ignore it. 3. Select a random 2–3 character sequence that looks pretty to you. 4. Check if anyone anywhere has used this sequence. 5. If so, return to step 3. 6.Congratulations! Enjoy you're new function.
Name:
Anonymous2009-11-13 16:20
>>41
For additional fun, you may append the str prefix, and you have the C string functions!