Remix.run Logo
hmry an hour ago

Why have operators at all?

  x = x.add(step.mul(2)).mod(width)
Or in C

  x = imod(iadd(x, imul(step, 2)), width)
vs

  x = (x + 2*step) % width
For me the answer is very simple: Operators make it easier to read the code which makes it easier to spot bugs. It also makes it easier to turn formulas from textbooks into code.

If 50% of the code you're working with is using vectors and matrices, not having operators for those parts is quite annoying.

Note that you can have vector operators without overloading, e.g. Odin has built in vector and matrix types.

But personally I think it's better to give the user more power instead of only letting the compiler author pick which types to allow operators on. Like how Java overloads + but only on the String class. Why do they get to do it, but not me?