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

The Swan language

Swan is the language you write inside Texl tags. It is small, dynamically typed and expression-oriented: a Swan program is a single expression, and evaluating it always produces a value. There are no statements, no keywords and no exceptions. An invalid operation does not stop the program; it produces an Undefined value instead.

This part of the manual describes the language. To learn how Swan fits into a template, start with Templates.

A quick tour

name = "Swan"                        # bind a name
"Hello, " + name + "!"               # => "Hello, Swan!"

double = x -> x * 2                  # define a function
double 21                            # => 42

(1, 2, 3) => double                  # => (2, 4, 6)
[10, 20, 30] 1                       # => 20

point = {x: 3, y: 4}                 # a namespace
point.x                              # => 3
math.sqrt (point.x ^ 2 + point.y ^ 2)  # => 5

age = 20
age >= 18 ? "adult" ; "minor"        # => "adult"

"hello" - 1                          # => Undefined

Reading the examples

Each example block is a short session. The lines run in order, and names bound on one line are available on the next. When a line ends with # =>, the text after the arrow is the value of that line, written in Swan notation:

ValueWritten as
numbers42, 3.14, inf
text"hello"
booleansTRUE, FALSE
nothing()
tuples(1, 2, 3)
lists[1, 2, 3]
namespaces{x: 3, y: 4}
functionsFunc
promisesPromise(42)
undefined valuesUndefined

A template shows some of these values differently. For example, a list renders as [[List of 3]]. See Tags and output.

Trying Swan

The quickest way to evaluate Swan expressions is a template. Save this as try.texl:

<% double = x -> x * 2 %><% double 21 %>

and render it with the command-line tool:

texl try.texl

Contents