News for hackers. Stuff that matters.
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)));

Re:Zig, the Small Language by Symmetry on Wednesday September 07, 2022 @03:03PM (#32754947)
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.
Re:Zig, the Small Language by masklinn on Wednesday September 07, 2022 @03:27PM (#32755431)
> 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.