Overloaded programmer’s brain



  • OK, so ...

    my $ezToday = new Date::EzDate('2013-06-06');
    my $ezYesterday = new Date::EzDate('2013-06-05');
    
    # Default arithmetic overload is epoch day (regardless of the time)
    say "Yes" if $ezToday == $ezYesterday + 1;
    say "Yes" if $ezToday == $ezYesterday + 1;

    Expected output:

    Yes
    Yes

    Actual output:

    Yes
    

    WTF?

    Turns out that the developer incorrectly implemented operator overloading. He overloaded + with a method that behaves as +=, and - with a method that behaves as -=. Adding or subtracting an integer from an EzDate object modifies the original object in the expression and returns it, instead of cloning it and returning a modified clone; comparisons such as the above consequently cause your logic to go bananas.

    (The only overloads defined were for +, -, <=> and stringify. Naturally, ++ and -- — which I do use, and which Perl automatically implements using the overloads available — work perfectly …)

    I was trying to decide whether the code was going nuts, or whether I was going nuts even more dozy than usual



  • @Daniel Beardsmore said:

    He overloaded + with a method that behaves as +=, and - with a method that behaves as -=.

    So you are you asking for method or alibi recommendations for murder?


  • Discourse touched me in a no-no place

    @Daniel Beardsmore said:

    Turns out that the developer incorrectly implemented operator overloading. He overloaded + with a method that behaves as +=, and - with a method that behaves as -=. Adding or subtracting an integer from an EzDate object modifies the original object in the expression and returns it, instead of cloning it and returning a modified clone; comparisons such as the above consequently cause your logic to go bananas.
    So, after that little bit of testing logic you find that yesterday is actually tomorrow?@Daniel Beardsmore said:
    (The only overloads defined were for +, -, <=> and stringify. Naturally, ++ and -- — which I do use, and which Perl automatically implements using the overloads available — work perfectly …)
    Perl exists for people who think PHP is too long-winded and obvious.



  • Some people say that goto is evil, others say that eval is evil. Not me. I find true evil in overloading native operators/methods.



  • Adding or subtracting an integer from an EzDate object modifies the original object in the expression and returns it, instead of cloning it and returning a modified clone;

    But that's standard practice! When operating on bigger objects, you pass them by reference, so that the cloning doesn't hinder your performance! It's the most basic optimization!



  • It's standard practice to overload "+" to modify its own operands?



  • This is probably using DateTime underneath. Apparently, $datetime_object->add('1 day') doesn't just return the next day, it modifies the object. You have to say $datetime_object->clone->add('1 day') instead. Ugh.



  • @realmerlyn said:

    This is probably using DateTime underneath. Apparently, $datetime_object->add('1 day') doesn't just return the next day, it modifies the object. You have to say $datetime_object->clone->add('1 day') instead. Ugh.

    Still, did nobody actually test it?



  • @morbiuswilters said:

    @realmerlyn said:
    This is probably using DateTime underneath. Apparently, $datetime_object->add('1 day') doesn't just return the next day, it modifies the object. You have to say $datetime_object->clone->add('1 day') instead. Ugh.

    Still, did nobody actually test it?

    Test it? [i]Test it?[/i]

    Hahahahahahaha! You're such a kidder!



  • @realmerlyn said:

    This is probably using DateTime underneath.

    It's all custom code. It has a few bugs, but generally it works well.

    I admit that I was hesitant about using clone(), but operator overloading within an expression A == B + C is not going to be optimal!



  • @Daniel Beardsmore said:

    It's standard practice to overload "+" to modify its own operands?

    Woosh.

     



  • I will never figure out what possessed the designers of C++ such that iostream is implemented this way.



  • @MiffTheFox said:

    I will never figure out what possessed the designers of C++ such that iostream is implemented this way.

    Eh, it's not a problem in itself (I actually like it, it's clean and easy to explain to beginners) - until you get to the chapter about bitwise operators and learn that the << and >> have precedence over all other ones.



  • @Daniel Beardsmore said:

    It's standard practice to overload "+" to modify its own operands?
    At least it seems common practice to teach it like that : I got that too in a "C++ initiation class" by my Systems and C teacher.

    No wonder I had to unlearn everything I knew about C++ before relearning it from a book.



  • @Medinoc said:

    @Daniel Beardsmore said:

    It's standard practice to overload "+" to modify its own operands?
    At least it seems common practice to teach it like that : I got that too in a "C++ initiation class" by my Systems and C teacher.

    No wonder I had to unlearn everything I knew about C++ before relearning it from a book.

    See kids, you have two apples and two apples. When you add them, you now have four apples and two apples. Now go crash the market.



  • @Daniel Beardsmore said:

    It's standard practice to overload "+" to modify its own operands?

    Yes. That's why after using

    $ezDate = $ezToday + 1;

    we find that 1 == 2.



  • @Medinoc said:

    @Daniel Beardsmore said:

    It's standard practice to overload "+" to modify its own operands?
    At least it seems common practice to teach it like that : I got that too in a "C++ initiation class" by my Systems and C teacher.

    No wonder I had to unlearn everything I knew about C++ before relearning it from a book.

    Not sure about your definition of "common".  Ever since Stroustrup's book, the standard prototype for overloading + is

     

    class T {
    ...
      const T operator+(const T& other) const;
    ...
    }

    which returns a fresh temporary.  Think your teacher must have been TRWTF.



  • @DaveK said:

    @Daniel Beardsmore said:

    It's standard practice to overload "+" to modify its own operands?

    Woosh.

     

    Would you be so kind, good sir, to enlighten me as to what it was that I missed?


  • Winner of the 2016 Presidential Election

    @Daniel Beardsmore said:

    Would you be so kind, good sir, to enlighten me as to what it was that I missed?

    Your sense of humor.



  • @joe.edwards said:

    Your sense of humor.

    Doh. I thought I was being told off. I couldn't be more insecure if I tried.



  • @Daniel Beardsmore said:

    @DaveK said:

    @Daniel Beardsmore said:

    It's standard practice to overload "+" to modify its own operands?

    Woosh.

    Would you be so kind, good sir, to enlighten me as to what it was that I missed?

    It appeared to me that you had missed that Maciejasjmj wasn't being serious.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.