Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators

Precedence

Operators with a higher precedence bind tighter. Use parentheses whenever the order is not obvious.

PrecedenceOperatorsMeaningAssociativity
highest+x -x !x $xprefix operators
30x.ysubcontextingleft
28f xfunction applicationleft
27x ^ ypowerright
26x * y x / y x % yproduct, division, moduloleft
25x + y x - ysum, subtractionleft
24== != < <= > >=comparisonleft
23x & y x | ylogical and, orleft
22x ? yconditionalleft
21x ; yalternativeleft
15x -> yfunction definitionright
14f >> g g << fcompositionleft
13x => fmappingright
13p ~> fthenleft
12x = y x : yassignment, labellingleft
lowestx , ypairingleft

Some consequences:

double = x -> x * 2
double 2 + 3         # => 7, because application binds tighter than +
double (2 + 3)       # => 10
2 ^ 3 ^ 2            # => 512, because ^ is right-associative
-2 ^ 2               # => 4, because prefix - binds tighter than ^
-(2 ^ 2)             # => -4
x = 1, 2             # => 2, because , binds looser than =
x                    # => 1

Prefix operators bind tighter than everything, even . and application. Wrap the operand in parentheses when it is more than a name or a literal:

-math.PI             # => Undefined, because it means (-math).PI
-(math.PI)           # => -3.141592653589793

Arithmetic

OperatorNumbersTextListsBooleans
x + ysumconcatenationconcatenationlogical or
x - ysubtraction
x * yproductlogical and
x / ydivision
x % yremainder
x ^ ypower

Both operands must have the same type. Any other combination gives Undefined. Swan never converts types implicitly.

12 + 2 * 3           # => 18
7 % 3                # => 1
-7 % 3               # => -1, the sign of the left operand
"Te" + "xl"          # => "Texl"
[1, 2] + [3]         # => [1, 2, 3]
"Total: " + 10       # => Undefined
"Total: " + text.from 10   # => "Total: 10"

Prefix -x negates a number. Prefix +x returns x unchanged, whatever its type.

Comparison

==, !=, <, <=, > and >= compare two values and return TRUE or FALSE. See Equality and Order for the rules. Chained comparisons do not work: a < b < c compares a boolean with c. Write a < b & b < c instead.

Logic and conditions

OperatorResult
!xFALSE if x is true, TRUE if it is false.
x & yx if x is false, otherwise y.
x | yx if x is true, otherwise y.
x ? yy if x is true, otherwise Nothing.
x ; yx unless x is Nothing; then y.

In all four, y is evaluated only if it is needed. &, | and ? test x as a whole, even when it is a tuple: they do not work item by item.

Together, ? and ; make an if-else: cond ? then ; else. See Conditions and logic.

Functions

OperatorResult
x -> yA function with parameters x and body y.
f xf applied to x. Lists, text and namespaces can be applied too.
f >> gA function that applies f, then g to the result.
g << fThe same function: g after f.
x => ff applied to each item of the tuple x.

See Functions.

Names and namespaces

OperatorResult
x = yBinds the name(s) x to y, and returns Nothing.
x : yBinds the name(s) x to y, and returns y.
ns.xEvaluates the expression x inside the namespace ns.
$xThe value of the name x in the outermost scope that binds it.

See Names and scopes and Namespaces.

Tuples and promises

OperatorResult
x , yA tuple of the items of x followed by the items of y.
p ~> fA promise of f applied to the value of the promise p.

See Tuples and Promises.

Operators on tuples

All binary operators except ,, ;, =, :, ->, => and ~> work item by item when an operand is a tuple. The prefix operators - and ! do too.

(1, 2) + (10, 20)    # => (11, 22)
-(1, 2)              # => (-1, -2)

A single value counts as a tuple of one item. It is not repeated to match the other operand, so use mapping to apply the same operation to every item:

(1, 2, 3) * 10                 # => (10, Undefined, Undefined)
(1, 2, 3) => (x -> x * 10)     # => (10, 20, 30)

See Operations on tuples for the details.