Operators View Source
This document covers operators in Elixir, how they are parsed, how they can be defined, and how they can be overridden.
The following is a list of all operators that Elixir is capable of parsing, ordered from higher to lower precedence, alongside their associativity:
Elixir provides the following built-in operators that are defined as functions that can be overridden:
-- unary positive/negative
/- basic arithmetic operations
--- list concatenation and subtraction
&&- strict and relaxed boolean "and"
||- strict and relaxed boolean "or"
!- strict and relaxed boolean "not"
not in- membership
@- module attribute
..- range creation
<>- binary concatenation
=~- text-based match
Many of those can be used in guards; consult the list of allowed guard functions and operators.
Additionally, there are a few other operators that Elixir parses but doesn't actually use. See Custom and overridden operators below for a list and for guidelines about their use.
Some other operators are special forms and cannot be overridden:
Finally, these operators appear in the precedence table above but are only meaningful within certain constructs:
Elixir provides the following built-in comparison operators (all of which can be used in guards):
==- equal to
===- strictly equal to
!=- inequal to
!==- strictly inequal to
<=- less-than or equal to
>=- greater-than or equal to
iex> 1 == 1.0 true iex> 1 === 1.0 false
In Elixir, different data types can be compared using comparison operators:
iex> 1 < :an_atom true
The reason we can compare different data types is pragmatism. Sorting algorithms don't need to worry about different data types in order to sort. For reference, the overall sorting order is defined below:
number < atom < reference < function < port < pid < tuple < map < list < bitstring
When comparing two numbers of different types (a number being either an integer or a float), a conversion to the type with greater precision will always occur, unless the comparison operator used is either
!==. A float will be considered more precise than an integer, unless the float is greater/less than +/-9007199254740992.0 respectively, at which point all the significant figures of the float are to the left of the decimal point. This behavior exists so that the comparison of large numbers remains transitive.
The collection types are compared using the following rules:
- Tuples are compared by size, then element by element.
- Maps are compared by size, then by keys in ascending term order, then by values in key order. In the specific case of maps' key ordering, integers are always considered to be less than floats.
- Lists are compared element by element.
- Bitstrings are compared byte by byte, incomplete bytes are compared bit by bit.
- Atoms are compared using their string value, codepoint by codepoint.
Elixir is capable of parsing a predefined set of operators; this means that it's not possible to define new operators (like one could do in Haskell, for example). However, not all operators that Elixir can parse are used by Elixir: for example,
|| are used by Elixir for addition and boolean or, but
<~> is not used (but valid).
To define an operator, you can use the usual
def* constructs (
defmacro, and so on) but with a syntax similar to how the operator is used:
defmodule MyOperators do # We define ~> to return the maximum of the given two numbers, # and <~ to return the minimum. def a ~> b, do: max(a, b) def a <~ b, do: min(a, b) end
To use the newly defined operators, we have to import the module that defines them:
iex> import MyOperators iex> 1 ~> 2 2 iex> 1 <~ 2 1
The following is a table of all the operators that Elixir is capable of parsing, but that are not used by default:
Note the Elixir community generally discourages custom operators. They can be hard to read and even more to understand, as they don't have a descriptive name like functions do. That said, some specific cases or custom domain specific languages (DSLs) may justify these practices.
It is also possible replace predefined operators, such as
+, but doing so is extremely discouraged.