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

Undefined values

When an operation makes no sense, such as subtracting a number from a text, Swan does not stop. The operation returns an Undefined value, which records what went wrong, and the evaluation continues.

"hello" - 1          # => Undefined
type ("hello" - 1)   # => "Undefined"

In a template, an undefined value renders as [[Undefined]].

Where they come from

  • Operators applied to the wrong types: "a" + 1, -"a", TRUE < 1.
  • Applying a value that cannot be applied: 5 3.
  • Built-in functions given the wrong arguments: text.size 42.
  • Math results that are not numbers: math.sqrt (-1).
  • Syntax errors: (1 + 2, 1 + * 2, 2 + 3 ) 1.
  • A recursion that goes too deep, or never stops: "recursion too deep".
  • Binding something that is not a name: 1 = 2.
  • Binding a name inside a namespace: math.(pi = 3).
  • undef.from, which wraps any value in an undefined value.
  • texl.import of a document that cannot be loaded.
  • A function provided by the program that runs Texl (the host), when it fails.

An unbound name is not undefined: it is Nothing.

How they spread

Most operations with an undefined operand give an undefined result, so an error early in an expression usually shows in its final value:

("hello" - 1) + 5    # => Undefined
text.upper ("hello" - 1)       # => Undefined

A few operations handle them like other values:

  • An undefined value is false, so ?, &, | and ! treat it as such.
  • ; does not replace it, because it is not Nothing.
  • A tuple keeps it as one of its items.
!("hello" - 1)       # => TRUE
("hello" - 1) | 0    # => 0
("hello" - 1) ; 0    # => Undefined
(1, "a" - 1, 3)      # => (1, Undefined, 3)

So | is the way to replace an undefined value with a default.

Finding out what went wrong

undef.inspect turns an undefined value into a namespace that describes it:

err = undef.inspect ("hello" - 1)
err.type             # => "BinaryOperation"
err.op               # => "subtraction"
err.lhv              # => "hello"
err.rhv              # => 1

description gives the error in plain words, here: cannot subtract the number 1 from the text "hello". The other members depend on type:

typeCauseOther members
UnaryOperationa prefix operator or a built-in functionop, val, pos
BinaryOperationa binary operator, an application or a failed loadop, lhv, rhv, pos
Terminationthe expression ended before its bracket was closedexpected, pos
Tokena character or token in the wrong placetoken, pos
Namea name was expectedpos
Valuecreated with undef.fromval
HostErrora function of the host failederror, pos

op names the operation, such as "sum", "application", "text.size" or "document.load". error is the text of the host's error. pos is the position of the error, counted in bytes from the start of the document, and uri is the document it is in. An error inside a function imported from another document points into that document. An error made by a built-in function points to its call. In code run with parse, pos counts from the start of that code, and there is no uri. A value made with undef.from has no position.

When an operand is itself undefined, undef.inspect describes it too, so you can follow the chain back to the original error.

Creating undefined values

A function can return an undefined value to signal that its input was invalid. undef.from wraps any value, such as a message, into one:

safe_div = (a, b) -> b == 0 ? undef.from "division by zero" ; a / b
safe_div (1, 0)      # => Undefined
(undef.inspect (safe_div (1, 0))).val    # => "division by zero"