Language Design: Unary Operators

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)

: Logical complement (on booleans) ~ : Bitwise complement (on numbers)

: Bitwise complement (on numbers) - : Numeric 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. Considering they provide rather limited benefits – while adding complexity to the core language – it is questionable whether unary operators are a good place to spend a language’s complexity budget on.

An alternative is to define methods on the respective types, dropping unary operators altogether:

not replaces ! on booleans

replaces on booleans not replaces ~ on numbers

replaces on numbers negate replaces - on numbers



This also elegantly solves the question whether

let x = 1 - x . abs

evaluates to 1 or -1 , by requiring users to write x.negate.abs – thereby leaving no ambiguity to precedence.

There are two additional benefits to the use methods instead of operators: