logoalt Hacker News

ozgrakkurt • today at 9:37 AM • 6 replies • view on HN

Here is a crazy idea, don’t use operators, just use functions.

Operators cause issues because it is not possible to cleanly express error output from an operator, it is not possible to express the bit-width of the operation, have to understand precedence.

All of these issues cause other downstream issues.

Why not add_u64()?


Replies

anonymous908213 • today at 9:58 AM

Because it's ridiculously verbose. Abbreviating extremely common operations makes code far easier to read and understand than not.

> have to understand precedence

Precedence is not in any way an argument against using operators; you could disallow precedence at all (eg. each expression must have at most one operator, or rather two counting the assignment operator) while still having operators. That said, precedence is also very beneficial to readability when not abused.

> Operators cause issues because it is not possible to cleanly express error output from an operator

Incorrect. An operator can simply desugar to functions, and therefore has the exact same error semantic possibilities. If you take issue with a language not returning errors from operators, that is a language design issue, not something inherent to the existence of operators.

➕ show 1 reply
layer8 • today at 12:37 PM

For even cleaner expression, let’s just draw the whole program as a syntax tree.

shiandow • today at 10:44 AM

Personally I think the meaning of + and * should be context dependent.

Though the main reason for infix notation is to signal associativity. Which a lot of these don't satisfy.

A lot of these are just trying to mimic natural language, which can be useful but as lisp demonstrates is not that important if the grammar is clear enough.

➕ show 1 reply
solatic • today at 10:03 AM

Zig sometimes gives you this, for example there are @divCeil, @divTrunc, @divExact, and @divFloor built-ins so that you can be more precise in your intent compared to using "/". It's rather simple to then write a linting rule that fails if you ever have a literal "/" in your codebase.

flohofwoe • today at 10:30 AM

...and while at why not also dump the assignment and indexing operators!

    assign(result, add(b, mul(c, sub(d, e)), add(add(f, g), array_get(arr, i)));
(I bet I messed up the parentheses somewhere).

At that point, even assembly code is more readable. Also there's a reason why Lisp and Forth haven't exactly taken the world by storm ;)