Re:Zig, the Small Language
by WalterBright on Wednesday September 07, 2022 @01:21PM
(#32753433)
Here's a sweet example of proper use of operator overloading:
https://dlang.org/phobos/std_checkedint.html
where operator overloading is used to create variations on integer types, like specifying the behavior when overflow happens.
Besides, `a + b / (c * d)` is far more readable than `add(a, div(b, mul(c, d)));
I think haskell does this right. You can make function infix by turning "add a b" into "a `add` b" and you can't overload existing operators but you can make your own and do "a _+ b" or whatever and everybody will know it isn't the standard + but it's still readable.
> you can't overload existing operators
You can though, just implement the relevant typeclass.
Not only that, but you can straight up shadow existing operators.
Re:Zig, the Small Language
by skocznymroczny on Wednesday September 07, 2022 @02:25PM
(#32754287)
It makes a massive difference. I find most math related code unreadable without operator overloading, such code is very common in e.g. gamedev.
Just because people misuse operator overloading doesn't mean it should be removed from the language.
But we are using operator overloading already, + does work for the basic numeric types doesn't it? So why should the line be drawn at float or double (zig didn't have a complex type last I looked), why not vec?
Re:Zig, the Small Language
by adgjlsfhk1 on Wednesday September 07, 2022 @01:18PM
(#32753409)
readability is important. specifically, operator overloading is great because it makes code look more like math. when translating from math to code, this is highly useful.
i'm not asking for operator _overloading_, i am asking for having operators for my math types
why? operator overloading doesn't help you solve any problems. you can have the readability with methods which are named appropriately.
operator overloading seems so powerful and useful until you realize one day that it only changes the appearance of things, and makes no difference whatsoever to anything you are actually doing.