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

Root names

These names are bound directly in the global scope.

TRUE, FALSE

TRUE -> Bool
FALSE -> Bool

The two boolean values. They are also available as bool.TRUE and bool.FALSE.

TRUE & FALSE         # => FALSE

type

type value -> Text

Returns the name of the type of value: "Bool", "Numb", "Text", "List", "Namespace", "Func", "Promise" or "Undefined".

Like other functions, type is applied to each item of a tuple, so it never returns "Tuple" or "Nothing": type (1, "a") is ("Numb", "Text"), and type () is ().

type 42              # => "Numb"
type [1, 2]          # => "List"
type text.size       # => "Func"
type (1, "a")        # => ("Numb", "Text")

parse

parse source -> Func

Compiles the Swan expression in the text source and returns a function. The function takes a namespace and evaluates the expression inside it, so the expression can use the members of the namespace and the built-in names, but not the other names of the program. Like any expression inside a namespace, it cannot bind names there.

The source is parsed once, when parse is called. A syntax error does not make parse fail: the function returns Undefined when it runs. A source that is not text gives Undefined, and so does applying the function to something other than a namespace.

area = parse "width * height"
area {width: 3, height: 4}     # => 12
area 42                        # => Undefined

bind

bind name value -> Nothing

Binds name to value in the global scope, wherever it is called from, and returns Nothing. name is a text, or a tuple of texts to bind several names at once, following the rules of destructuring.

bind is how a function, or a callback waiting on a promise, makes a value available to the rest of the program. It binds the name in the global scope even when it is called inside a namespace: it never changes a namespace. In a template, the global scope is the scope of the document, where its tags run.

f = () -> bind "seen" TRUE
f()
seen                           # => TRUE
bind ("a", "b") (1, 2)
a + b                          # => 3

label

label name value -> value

Like bind, but returns value instead of Nothing, the way : differs from =.

label "answer" 42              # => 42
answer                         # => 42