• red_tomato@lemmy.world
    link
    fedilink
    arrow-up
    22
    arrow-down
    1
    ·
    20 days ago

    Operator overloads is something that sounds like a good idea until you experience them firsthand. Just properly name your functions for fuck’s sake.

    • balsoft@lemmy.ml
      link
      fedilink
      arrow-up
      8
      ·
      20 days ago

      Operator overloading is awesome in a functional language. Haskell allows for a lot of creative stuff with operators, but since they can’t have side effects, must conform to predictable types, and have specific requirements they are somewhat understandable. Idris is also very ergonomic when it comes to operators.

      C++ allowing a library to override the + operator with system("rm -rf /") is a lot less awesome.

    • unknownuserunknownlocation@kbin.earth
      link
      fedilink
      arrow-up
      5
      arrow-down
      1
      ·
      20 days ago

      Honestly, I’m glad to have them, it can make a number of things simpler. For instance, a program I wrote had a number of duration fields that had to be stored in XML files and updated every once in a while. It was something that had to occur very often in the code: add a value to the duration, set that value in the XML data, and save the XML file. So I created a class that stored the date/time value and a pointer the XML element (it could be assumed that nothing else would be changing the value in the XML file and if it did, then that change should be overwritten) and overloaded the += operator (among others), so then all I needed was to create the DurationField object, and then I could go durationFieldX += time2 - time1 for instance. I could abstract all the logic away and make it very clear and easy to write. Could I have made it so that I have to to durationFieldX.add(time2 - time1)? Of course I could have. But IMO that variant is less neat and less readable.

      • MangoCats@feddit.it
        link
        fedilink
        arrow-up
        4
        ·
        20 days ago

        I’ll jump on your unpopular side. With a decent IDE you should be able to point/click and decode what any operator is doing in any context. If I have two vectors, or quaternions, and I want to operate on them, why should I be forced to pass them to a standard function instead of clearly seeing what I’m doing. If you can’t be bothered to know the types of your variables in a strongly typed language… IDK.

    • Chrobin@discuss.tchncs.de
      link
      fedilink
      arrow-up
      4
      ·
      20 days ago

      As a computational physicist, I appreciate operator overloading IF used properly.
      Example of a good use: + for matrix addition
      Example of a bad use: left shift operator << as a pipe
      I still don’t understand who’s in the C++ committee that they would choose this garbage over a function