3 ms·
Yeah I never got the aversion to operator overloading either. "+ can do anything!" As you said, so can plus(). "Hidden function calls?" Have they never progra
by bigpingo 2y ago
Yeah I never got the aversion to operator overloading either.
"+ can do anything!" As you said, so can plus().
"Hidden function calls?" Have they never programmed a soft float or microcontroller without a div instruction? Function calls for every floating point op.
- mk12 2y agoThe problem is not that + calls a function. The problem is that + could call one of many different functions, i.e. it is overloaded. Zig does not allow overloading plus() based on the argument types. When you see plus(), you know there is exactly one function named “plus” in scope and it calls that.
- kps 2y agoNot if `plus` is a pointer. Then `plus()` is a conditional branch where the condition can be arbitrarily far away in space (dynamically scoped) and time. That's why I think invisible indirection is a mistake. (C used to require `(*plus)()`.)
- gpderetta 2y agoor 'plus' could be a macro...
- gpderetta 2y agooperator + is overloaded even in plain C: it will generate different instructions for pointers, floats, integers, _Complex, _Atomic and the quasi standard __float128. Sometimes it will even generate function calls. I suspect zig might be similar.