Comment On Operator Overloading Overload

The ability to define how operators behave on structures and classes is one of the cooler features available in modern programming languages. It's one of those things that everyone knows how to do but no one has actually done. Aside, of course, from writing that String class back in CS-215: C++ Programming. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Operator Overloading Overload

2005-07-29 13:44 • by zephc
Hah, i like how s/he used os.path.join() in the constructor. Maybe the guy was a Perl programmer, and wanted MTOWTDI. I also like how that class was only used a few times in the project, further invalidating the time spent on it.

Operator overloading definitely has its advantages, but this is just silly.

6/10 on the WTF meter, IMO.

Re: Operator Overloading Overload

2005-07-29 13:49 • by Daruku
huh?

Re: Operator Overloading Overload

2005-07-29 14:08 • by Ytram
I'm dying to know what language this is.  It looks like the
bastard child that would come from an unholy union of C++ and VB

Re: Operator Overloading Overload

2005-07-29 14:11 • by Dave
39462 in reply to 39460
It's python, which is actually the bastard child of Java and Perl.

Re: Operator Overloading Overload

2005-07-29 14:12 • by aberant
looks like python to me...

Re: Operator Overloading Overload

2005-07-29 14:13 • by Davidp
I'm sorry but this WTF is too obscure and dense to parse.  Where exactly are the WTFs?

Re: Operator Overloading Overload

2005-07-29 14:13 • by JAFO
39465 in reply to 39460
This is Python, sweet glorious python and until you get in and do it, you won't realize what a beautiful and powerful language it is.

The syntax is a breeze and quite readable as a general rule.

Re: Operator Overloading Overload

2005-07-29 14:17 • by YAFO
39466 in reply to 39465
The WTF is that every operator, accessors and mutators, are overridden to actually append to the string. The comment alone explains it.

I think that "least surprise" principle was violated severly, and will be aching for days.

Re: Operator Overloading Overload

2005-07-29 14:20 • by Laz
39467 in reply to 39464
Anonymous:
I'm sorry but this WTF is too obscure and dense to parse.  Where exactly are the WTFs?




^^^ What he said...

Re: Operator Overloading Overload

2005-07-29 14:20 • by JAFO
39468 in reply to 39462
Anonymous:
It's python, which is actually the bastard child of Java and Perl.

No, not at all. Python began in 1990. Java/Oak began in 1991

Short python overview

Re: Operator Overloading Overload

2005-07-29 14:20 • by Ayende Rahien
39469 in reply to 39464
Well, there is the use of operator overloading when you don't need them.

There is the aweful syntax that you get path("Users")["Ayende"](Desktop)() ???!!!

Re: Operator Overloading Overload

2005-07-29 14:28 • by Unindicted coconspiritor
39470 in reply to 39457
<i>Maybe the guy was a Perl programmer, and wanted MTOWTDI.</i>



Ding ding ding ding ding!

Re: Operator Overloading Overload

2005-07-29 14:48 • by Jake P
Ok, I'll bite and defend this one.



The general idea isn't too bad.  If they get rid of __getattr__
and replace __add__ with __div__, they get a program where operator
overloading actually makes some sense.  At the very least, it
makes paths more readable:





a = path("a");

b = path("b");

c = a/b;





Been done before by others... I would just use the standard path manipulators myself.



Re: Operator Overloading Overload

2005-07-29 15:00 • by endothermal
39472 in reply to 39471

Anonymous:
Ok, I'll bite and defend this one.

The general idea isn't too bad.  If they get rid of __getattr__ and replace __add__ with __div__, they get a program where operator overloading actually makes some sense.  At the very least, it makes paths more readable:


a = path("a");
b = path("b");
c = a/b;


Been done before by others... I would just use the standard path manipulators myself.


assuming c would equal "a/b" or a simpler example


a=path("/inittech");


b=path("superprogram")


c=a/b


c would then equal  "/inittech/superprogram"


I see your point with the / operator, but it really would confuse the hell out of a new programmer reading the code for the first time.  Unless the consumer of the class has access to the code I would probably avoid using operator overloading like that.

Re: Operator Overloading Overload

2005-07-29 15:05 • by zephc
I also wanted to say that if you've never seen Python before, don't let this WTF taint it for you. It's a great language, and this is a terrible way to experience it. Python tends to be very readable, yet powerful.

To explain each part:
The methods with the two underscores are reserved in Python such that

"this is" + " my name"
is the same has
"this is".__add__(" my name")
(Aside: Notice that literals can have methods called upon them)

http://www.network-theory.co.uk/docs/pylang/ref_32.html has a good summary of these reserved methods.

Re: Operator Overloading Overload

2005-07-29 15:25 • by Charles Nadolski
39474 in reply to 39470
Anonymous:
Maybe the guy was a Perl programmer, and wanted MTOWTDI.



Ding ding ding ding ding!





For those too lazy to google, the acronym means
More
Than One Way To Do It, which I thought was something to AVOID in
programming. Even in C++ I get annoyed that you can use *(SomeArray+i)
and SomeArray[i] in the same statement, which do the same thing but are
two different concepts (pointer arithmetic versus array
iteration).  Now you can use 10 different syntaxes to put strings
together! WOOT!


Re: Operator Overloading Overload

2005-07-29 15:30 • by DannyB
39475 in reply to 39474
In C and C++, you can actually use i[SomeArray].



Try it!



Re: Operator Overloading Overload

2005-07-29 15:52 • by Ytram
39476 in reply to 39473
zephc:
(Aside: Notice that literals can have methods called upon them)




.NET and Java both support this as well.  It can be a useful way to avoid null reference exceptions.

Re: Operator Overloading Overload

2005-07-29 16:02 • by Sean Connery
39478 in reply to 39457
zephc:
Hah, i like how s/he used os.path.join() in the
constructor. Maybe the guy was a Perl programmer, and wanted MTOWTDI. I
also like how that class was only used a few times in the project,
further invalidating the time spent on it.
Operator overloading definitely has its advantages, but this is just
silly.
6/10 on the WTF meter, IMO.




Tell Boost that http://www.boost.org/libs/filesystem/doc/

Re: Operator Overloading Overload

2005-07-29 16:06 • by Jake
39480 in reply to 39475
Anonymous:
In C and C++, you can actually use i[SomeArray].



Try it!






Thats because all the [] operator does is take your i and your
SomeArray, add them together and dereference the result.  Since
addition is commutative, you get interesting results such as you
pointed out.

Re: Operator Overloading Overload

2005-07-29 16:07 • by JamesCurran
39481 in reply to 39475

Anonymous:
In C and C++, you can actually use i[SomeArray].



Anonymous:
(Aside: Notice that literals can have methods called upon them)


And of course we can combine these so that


"ABCD"[2] == 2["ABCD"] == 'C'


 

Re: Operator Overloading Overload

2005-07-29 16:25 • by WTFer
And of course if we are talking of the ugly curiosities of C we can never forget the Duff Device.

Re: Operator Overloading Overload

2005-07-29 18:05 • by pycoder
39483 in reply to 39482
__getitem__ = __getattr__
Auch , beautifull WTF !
It must be a perl or php coder


Re: Operator Overloading Overload

2005-07-29 18:06 • by Hank Miller
Thats it?   It doesn't do anything.  



I could see many reasons to write a class to do this, but all of them
abstract something system specific.   For instance if it
wrapped the path to the configuration file, and made sure it was
hidden, no matter what the OS, this would make sense (that is when
accessed it set the hidden bit on Windows, while starting the filename
with a '.' on Unix.)



path("project")a.b.c.super() -> "project/a/b/c/super"

God help the unix person with no python skills who is thinking

"project/a.b.c.super"


Of for that matter someone who is thinking a that this is violating
abstraction by going down a long chain of member classes to call
c.super()

Re: Operator Overloading Overload

2005-07-29 19:34 • by Aristotle Pagaltzis
39488 in reply to 39457
zephc:
Maybe the guy was a Perl programmer, and wanted MTOWTDI.

Err, unlikely. No Perl programmer would use overloading to begin with. And any self-respecting Perl programmer relies on File::Spec, which Python’s os.path roughly corresponds to.



Larry’s own corollary to TMTOWTDI is “but not all ways are created equal.” The multiple options exist so you can pick the one which most clearly conveys intent, not to make your code more varied and exciting. If you don’t trust your restraint and sense of style, then not having options will obvious be desirable and Perl is not for you.



This WTF smells like a C++ programmer’s doing to me.

Re: Operator Overloading Overload

2005-07-29 20:11 • by loneprogrammer
39490 in reply to 39488
All the people trying to plug their favorite language need to shut up.



The only language that matters is binary code.  Everything else is just shorthand for it.



Re: Operator Overloading Overload

2005-07-29 21:49 • by Xepol
Uh, for those wondering where the wtf is, its because path("project", "super")() is probably longer to type than "project/super", makes you wonder, in fact, there does not appear to be ANY circumstances where this reduces typing, simplifies code or avoids bugs. Its more of a why than a what, as in why the F* would anyone bother?!?

Err,as to the suggesting to google MTOWTDI, why would I want to google a headroll on the keyboard? TIAPAWAGTLTBMOU! (There is a point at which acronyms get too long to be meaningful or useful.)

Mind you, having been through the education system with REAL programming experience before hand (I was the annoying guy there to get the piece of paper after already having a consulting career), I have to say, most institutions turn out people barely able to write hello world and expect them to get 60K a year jobs. It is no surprise that a lot of code looks like it was written by a spastic chimp.

Re: Operator Overloading Overload

2005-07-29 22:22 • by tim the enchanter
39495 in reply to 39490
loneprogrammer:
The only language that matters is binary
code.  Everything else is just shorthand for it.




actually, they're longhand for it. the binary code is the base level,
the languages on top are more complex and there to make life 'better'
for anyone that doesn't have bits for brains.

Re: Operator Overloading Overload

2005-07-29 23:16 • by Aristotle Pagaltzis
39497 in reply to 39494
Xepol:
Uh, for those wondering where the wtf is, its because path("project", "super")() is probably longer to type than "project/super", makes you wonder, in fact, there does not appear to be ANY circumstances where this reduces typing, simplifies code or avoids bugs.

Err. Try again. On Windows, that would be project\super. And MacOS Classic, it would be projects:super. That is what os.path.join is all about: system-independent filename manipulation. The WTF is that he writes an elaborate bunch of crap to be able to say path("project")["super"] where it should be simply os.path.join("project","super").

Re: Operator Overloading Overload

2005-07-29 23:27 • by phelyan
39498 in reply to 39497
Aristotle Pagaltzis:
Xepol:
Uh, for those wondering where the wtf is, its because path("project", "super")() is probably longer to type than "project/super", makes you wonder, in fact, there does not appear to be ANY circumstances where this reduces typing, simplifies code or avoids bugs.

Err. Try again. On Windows, that would be project\super. And MacOS Classic, it would be projects:super. That is what os.path.join is all about: system-independent filename manipulation. The WTF is that he writes an elaborate bunch of crap to be able to say path("project")["super"] where it should be simply os.path.join("project","super").



What gets me is the fact that he actually uses os.path.join in the code...

Re: Operator Overloading Overload

2005-07-30 00:27 • by Fishy5
39499 in reply to 39498
phelyan:
Aristotle Pagaltzis:
Xepol:
Uh, for those wondering where the wtf is, its because path("project", "super")() is probably longer to type than "project/super", makes you wonder, in fact, there does not appear to be ANY circumstances where this reduces typing, simplifies code or avoids bugs.

Err. Try again. On Windows, that would be project\super. And MacOS Classic, it would be projects:super. That is what os.path.join is all about: system-independent filename manipulation. The WTF is that he writes an elaborate bunch of crap to be able to say path("project")["super"] where it should be simply os.path.join("project","super").



What gets me is the fact that he actually uses os.path.join in the code...


That would be because he can't assume that he needs to use "/".

Re: Operator Overloading Overload

2005-07-30 05:26 • by Simon
39501 in reply to 39471
Anonymous:
Ok, I'll bite and defend this one.



Been done before by others... I would just use the standard path manipulators myself.






Indeed. The path
Python module
 is a good example.




Re: Operator Overloading Overload

2005-07-30 16:52 • by Ralph Loizzo
39503 in reply to 39475
You know, I forgot about that i[SomeArray]...



can anyone explain to me again why this works in c/c++?



index[SomeArray]

it seems like it should ONLY work as SomeArray[index]



What's going on behind the scenes?

Re: Operator Overloading Overload

2005-07-30 16:53 • by Ralph Loizzo
39505 in reply to 39503
Oops...



should have scrolled down for the answer!



Sorry...

Re: Operator Overloading Overload

2005-07-31 08:17 • by e
39509 in reply to 39460

Ytram:
I'm dying to know what language this is.  It looks like the bastard child that would come from an unholy union of C++ and VB


People seem to ask this every time. How about including which language it is in the post description for each WTF?

Re: Operator Overloading Overload

2005-07-31 09:32 • by DZ-Jay
39510 in reply to 39503
Anonymous:
You know, I forgot about that i[SomeArray]...



can anyone explain to me again why this works in c/c++?



index[SomeArray]

it seems like it should ONLY work as SomeArray[index]



What's going on behind the scenes?




Its actually pretty simple and straightforward:



In the expression SomeArray[index], the SomeArray part represents (i.e. translates/compiles to) a pointer to the base memory location of the array SomeArray, and the index part represents an offset from this base location.  So the expression evaluates finally to a pointer to "base-location SomeArray + offset index".



Conversely, in the expression index[SomeArray], the index part is evaluated as a pointer to the base memory location of a supposed array called index, while the SomeArray part is evaluated as an offset from this base location.  This expression represents a pointer to "base-location index + offset SomeArray".



The reason that this works is because, in a very technical way, it is
just pointer arithmetic, and by the commutative property of addition, "SomeArray + index" = "index + SomeArray".



I say in a "very technical way", because conceptually, "SomeArray + index" (i.e. SomeArray[index]) is the correct way of viewing at it, as the index[SomeArray] makes no semantic sense.



    dZ.



Re: Operator Overloading Overload

2005-07-31 10:14 • by Irrelevant

Actually, I think that index[some_array] only works if sizeof(*some_array) == 1.


some_array[index] is the same as *(some_array + index * sizeof(*some_array)), not *(some_array + index). Implicitly casting a non-pointer to a pointer gets you a void *, so sizeof(*not_a_pointer) is the same as saying sizeof(void). This doesn't make sense, so is generally taken to be 1 as a fallback.


So, index[some_array] is good for obfuscating access to your char *s, but not much else.

Re: Operator Overloading Overload

2005-07-31 11:17 • by JamesCurran
39512 in reply to 39511
Irrelevant:

Actually, I think that index[some_array] only works if sizeof(*some_array) == 1.


Nope.  This correctly prints "3.0000":


int main(int argc, char* argv[])
{
      float arr[5] = {1.0,2.0,3.0,4.0,5.0};
      printf (
"%f", 2[arr]);
      return 0;
}

Re: Operator Overloading Overload

2005-07-31 15:26 • by J
39515 in reply to 39494
Err,as to the suggesting to google MTOWTDI, why would I want to google a headroll on the keyboard?

Come on fhqwhgads.

Re: Operator Overloading Overload

2005-07-31 18:07 • by Suomynona
39519 in reply to 39511
Irrelevant:

Actually, I think that index[some_array] only works if sizeof(*some_array) == 1.


some_array[index] is the same as *(some_array + index * sizeof(*some_array)), not *(some_array + index). Implicitly casting a non-pointer to a pointer gets you a void *, so sizeof(*not_a_pointer) is the same as saying sizeof(void). This doesn't make sense, so is generally taken to be 1 as a fallback.


So, index[some_array] is good for obfuscating access to your char *s, but not much else.





There is no implicit cast from non-pointer to pointer in C/C++, except
for the conversion of the compile-time constant 0 to an appropriate
pointer type in C++. And that doesn't produce a void
*
unless void * happens to be
appropriate in this particular expression. In C++, there is no pointer
type that can be converted to any other pointer type, thus converting
to that (hypothetical) type is not an option. Conceptually, that would
require a "bottom type", but C++ only has a "top type" (void), which
works in the opposite direction. As a side-note, void is, oddly enough,
abstract, "incomplete" even, although it needn't be. Only the bottom
type has to be abstract (or, alternatively, every type must include an
object compatible with bottom, like in Haskell).



As for the sizeof(...) == 1 comment: That's
incorrect, it will work with element types of any size.
i[p] is equivalent to
p[i] in C/C++, which in turn is equivalent to
*(p+i). There is no multiplication with
sizeof(*p), because pointer arithmetic already performs it implicitely!




Re: Operator Overloading Overload

2005-08-01 01:27 • by Drak
39521 in reply to 39519

A question about this wonderful C++ code...


I understand why array[index] works...


But why does index[array] not end up taking the pointer to index and adding the pointer to array, leading to a very random place in memory? How does it know not to dereference index, while in the first case it does dereference array... Or am I just confused this early on Monday morning??


Drak

Re: Operator Overloading Overload

2005-08-01 02:11 • by witch
39522 in reply to 39521
Drak:

A question about this wonderful C++ code...


I understand why array[index] works...


But why does index[array] not end up taking the pointer to index and
adding the pointer to array, leading to a very random place in memory?
How does it know not to dereference index, while in the first case it
does dereference array... Or am I just confused this early on Monday
morning??





Because it doesn't dereference the pointer array in the first place!
Array is the name of the address of array, so naming it already
specifies the address, no need to reference or 'get-address-of' here!



It's confusing at first, but when you get used to it.... you use it as little as possible.



Christ



Re: Operator Overloading Overload

2005-08-01 03:23 • by Aaron
39523 in reply to 39475
That's just so stupid, it's just funny! I do feel strangely enlightened though!

Re: Operator Overloading Overload

2005-08-01 05:43 • by Drak
39526 in reply to 39522

witch:

Because it doesn't dereference the pointer array in the first place! Array is the name of the address of array, so naming it already specifies the address, no need to reference or 'get-address-of' here!

It's confusing at first, but when you get used to it.... you use it as little as possible.

Christ


hmm, ok I think I understand.. so the 'value' of array is actually it's address in memory...


Drak


 

Re: Operator Overloading Overload

2005-08-01 05:47 • by dhromed
39527 in reply to 39523
How about defining a WTF operator, that looks like this:



...

Re: Operator Overloading Overload

2005-08-01 07:38 • by Cynos
39528 in reply to 39465


This is Python, sweet glorious python
and until you get in and do it, you won't realize what a beautiful and
powerful language it is.


The syntax is a breeze and quite readable as a general rule.





Exactly, the WTF is that he's taken something simple and elegant, and obfuscated the shit out of it.



There's a philosophy to Python,here's a copy.



Some lucky b'stard actually gets to work on a Python project and does this. *sigh*

Re: Operator Overloading Overload

2005-08-01 07:40 • by lucio
39529 in reply to 39509
Anonymous:

Ytram:
I'm dying to know what language this is.  It looks like the bastard child that would come from an unholy union of C++ and VB


People seem to ask this every time. How about including which language it is in the post description for each WTF?



People are just too lazy to go and find out. And this was an easy one, for there's a link to http://docs.python.org/lib/module-os.path.html [:D]


Sometimes the Java/C# confusion comes along. I usually set them apart by methodName (Java) instead of MethodName (.NET), when possible.

Re: Operator Overloading Overload

2005-08-01 07:49 • by Cynos
39530 in reply to 39499
Anonymous:
phelyan:

What gets me is the fact that he actually uses os.path.join in the code...


That would be because he can't assume that he needs to use "/".






*sigh* os.path.join() is os independent.... which means that you use it
exactly when you can't assume how you'll be creating paths..... did you
miss the post that said that?



*double sigh*

Re: Operator Overloading Overload

2005-08-01 08:14 • by JamesCurran
39531 in reply to 39530
Anonymous:
Anonymous:
phelyan:

What gets me is the fact that he actually uses os.path.join in the code...


That would be because he can't assume that he needs to use "/".


*sigh* os.path.join() is os independent.... which means that you use it exactly when you can't assume how you'll be creating paths..... did you miss the post that said that?


um... Whom are you talking to?  You are just repeating the same point Anonymous just made......

Re: Operator Overloading Overload

2005-08-01 09:46 • by travisowens
__________underscores______________make___________code__________more________________readable
« PrevPage 1 | Page 2Next »

Add Comment