Language Design: Unary Operators are Unnecessary
TL;DR: Use methods.
Many languages provide unary operators, usually written as a prefix to the value they apply to.
The most common unary operators are:
!: Logical complement (on booleans)
~: Bitwise complement (on numbers)
-: Numeric complement (on numbers)
+: useless (on numbers)
Except for reasons of tradition and familiarity, their privileged position in many languages is unnecessary. They provide rather limited benefits – while adding complexity to the core language.
Unary operators are a waste of a language’s complexity budget.
An alternative is to define methods on the respective types, dropping unary operators altogether:
This also elegantly solves the question whether
let x = 1 -x.abs
should evaluate to
is completely unambiguous.
There are two additional benefits to the use methods instead of operators:
Using methods instead of unary operators moves the negation closer to the thing being negated:
if language.users.map(_.lastName).contains("Smith").not then ... else .... The more traditional
if !language.users.map(_.lastName).contains("Smith") then ... else ...requires users to read the negation first, then read the condition to the end of the line to figure out what is being negated.1
Using methods instead of unary operators allows the use of more elaborate result types: While a
negateoperation may always succeed on arbitrary-precision numbers (
BigDec, …), the same operation on the more common fixed-size types (
Long, …) could benefit from returning an optional result to indicate that a negative value may lack a positive counterpart.
Incomplete list of languages and their interpretation of
|-1.abs||let x = 1; -x.abs|
The Rust community had a similar discussion about this topic. ↩