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 and Punctuation

This section defines all operator and punctuation tokens in Forge.

Arithmetic Operators

TokenNameExampleDescription
+Plusa + bAddition; string concatenation
-Minusa - bSubtraction; unary negation
*Stara * bMultiplication
/Slasha / bDivision
%Percenta % bModulo (remainder)

When both operands of / are integers, the result is an integer (truncating division). When either operand is a float, the result is a float.

The + operator is overloaded for string concatenation when both operands are strings.

Comparison Operators

TokenNameExampleDescription
==Equala == bEquality test
!=Not equala != bInequality test
<Less thana < bLess-than comparison
>Greater thana > bGreater-than comparison
<=Less than or equala <= bLess-than-or-equal
>=Greater than or equala >= bGreater-than-or-equal

All comparison operators return a Bool value. Strings are compared lexicographically.

Logical Operators

TokenNameExampleDescription
&&Logical ANDa && bShort-circuit conjunction
||Logical ORa || bShort-circuit disjunction
!Logical NOT!aUnary boolean negation

The keywords and and or are not reserved keywords in Forge. Logical operations use the symbolic && and || forms exclusively. The not keyword is also not reserved; use the ! prefix operator.

Both && and || use short-circuit evaluation: the right operand is not evaluated if the result can be determined from the left operand alone.

Assignment Operators

TokenNameExampleEquivalent
=Assignmentx = 5
+=Add-assignx += 5x = x + 5
-=Subtract-assignx -= 3x = x - 3
*=Multiply-assignx *= 2x = x * 2
/=Divide-assignx /= 4x = x / 4

Assignment and compound assignment operators require the left-hand side to be a mutable variable (declared with mut). Compound assignment is syntactic sugar for the expanded form.

Member Access and Navigation

TokenNameExampleDescription
.Dotobj.fieldField access, method call
..Range1..10Range constructor (exclusive end)

The dot operator accesses fields on objects and struct instances, and invokes methods. It binds very tightly (highest precedence among binary operators).

The range operator .. creates a range value from a start (inclusive) to an end (exclusive). It is used primarily with for loops and the range() built-in.

Pipe Operators

TokenNameExampleDescription
|>Pipex |> fPipe-forward: passes left as first argument to right
>>Pipe rightx >> fAlternate pipe operator

The pipe operator passes the value on the left as the first argument to the function on the right:

let result = [3, 1, 4, 1, 5]
    |> sort
    |> reverse

Spread Operator

TokenNameExampleDescription
...Spread[...arr, 4]Spreads array/object elements

The spread operator expands an array or object into individual elements within an array literal or object literal:

let a = [1, 2, 3]
let b = [...a, 4, 5]       // [1, 2, 3, 4, 5]

let base = { x: 1, y: 2 }
let ext = { ...base, z: 3 } // { x: 1, y: 2, z: 3 }

Arrow Operators

TokenNameExampleDescription
->Arrow< 13 -> "kid"Arm separator in when/match
=>Fat arrowOk(v) => say vArm separator in match

The thin arrow -> is used in when guard arms. The fat arrow => is used in match pattern arms. Both separate a pattern/condition from its corresponding body.

Special Operators

TokenNameExampleDescription
?Questionexpr?Error propagation (Result/Option)
@At@testDecorator prefix
&Ampersand(reserved)Reserved for future use
|BarCircle(r) | Rect(w, h)ADT variant separator

The ? postfix operator propagates errors: if the expression evaluates to Err(e), the enclosing function returns Err(e) immediately. If the expression is Ok(v), the ? unwraps it to v.

Delimiters

TokenNamePurpose
(Left parenFunction call, grouping
)Right parenClose function call, grouping
{Left braceBlock, object literal, interpolation
}Right braceClose block, object, interpolation
[Left bracketArray literal, index access
]Right bracketClose array, index access
,CommaSeparator in lists
:ColonKey-value separator, type annotation
;SemicolonOptional statement terminator

Operator Precedence

Operators are listed from highest to lowest precedence:

PrecedenceOperatorsAssociativity
1 (highest). (member access)Left-to-right
2() (call), [] (index)Left-to-right
3!, - (unary)Right-to-left
4*, /, %Left-to-right
5+, -Left-to-right
6..Left-to-right
7<, >, <=, >=Left-to-right
8==, !=Left-to-right
9&&Left-to-right
10||Left-to-right
11|>Left-to-right
12?Postfix
13 (lowest)=, +=, -=, *=, /=Right-to-left

Parentheses may be used to override the default precedence.