Comment on How helpful languages create bugs

qwertyasdef@programming.dev ⁨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.

source
Sort:hotnewtop