• qwertyasdef@programming.dev
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    1 year ago

    I disagree with the author on operator overloading. They claim that this function in C

    float foo(float a, float b) {
    	return a+b;
    }
    

    is perfectly clear because you know it’s doing floating point addition, while this function in Python isn’t

    def foo(a, b):
    	return a + b
    

    because you don’t know if it’s floating point addition, integer addition, or string concatenation, and what happens if the inputs are different types?

    I think that’s fundamentally mistaken. You could also ask of the C version if it’s doing normalized floating point addition, denormalized floating point addition, infinity addition, or NaN propagation. What happens if you mix different types of floats? And the answer is that it doesn’t matter. These are all just aspects of floating point addition. It returns the most sensible result in whatever format is best to hold that value, and you don’t need to worry yourself about how floats are stored under the hood.

    The same is true of the Python version. It doesn’t matter if it’s integer addition or floating point addition or string concatenation. Those are just different aspects of the addition operator and it returns the most sensible result in whatever type is best to hold that value.

    • cgtjsiwy@programming.dev
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      Agreed. I think operator overloading is a necessary feature in any math-oriented or general-purpose language. Being able to write formulae the same way as they’re written in the source paper is a huge boon to readability.

  • A_A@lemmy.world
    link
    fedilink
    arrow-up
    0
    arrow-down
    2
    ·
    1 year ago

    This post is overlaps …

    *This post overlaps
    I am not sure I want to read the rest.

    • YonatanAvhar@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      There are a ton of typos in this post, missing semicolons, “you” instead of “your”, “then” instead of “than”… that’s just the first sentence