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

Using the REPL

Start a persistent interactive Goblin session with:

$ goblin repl
Goblin REPL. Press Ctrl-D to exit.
>>>

Expressions print their value automatically; declarations and statements do not. Names, imports, functions, and types stay available until the session exits.

>>> 1 + 2 * 3
7
>>> var values = [1, 2, 3]
>>> values.push(4)
>>> values
[1, 2, 3, 4]
>>> import "math"
>>> math.sqrt(81)
9.0

Relative imports are resolved from the directory where the REPL starts, so start it from a project directory when testing a local module.

In the REPL os.args is [] and os.program is "". They do not expose the goblin binary's process arguments. Use goblin run when you need real script argv.

Multi-line input

When parentheses, brackets, or braces are unbalanced, the prompt changes to ... and the REPL keeps collecting lines. This is how to enter functions, types, loops, and try/catch blocks.

>>> func square(x) {
...     return x * x
... }
>>> square(12)
144

Enter a blank line to force evaluation of a malformed fragment, or press Ctrl-C to discard the input currently being collected.

The REPL does not run the checks a file gets before it runs, because a line may use names declared on earlier lines. The mistakes those checks catch are reported when the line runs instead, with the same message. Assigning to a name nothing has declared raises NameError (assignment to undefined identifier: total), including from inside a function, so a misspelled name does not silently create a global; declare a new name with var. Assigning to a type name or to a built-in such as print without var raises NameError as well, while var print = ... declares a binding that shadows the built-in.

Entering a type declaration again rebinds its name. Values made before keep the old type. The REPL checks each type's impls when the type is defined, so impl Shape {} reports that Shape is not a protocol right away.

Completion and history

Press Tab to complete visible names, keywords, and member paths. For example, after import "json", type json. and press Tab to see members. Completion only reads attributes; it does not call Goblin functions.

History is stored in ~/.goblin_history; use Up/Down to revisit entries and Ctrl-D to exit. os.exit() leaves the REPL with that status after running the deferred calls of the current input. Use the REPL for small experiments and API discovery, then move repeatable work into a .goblin file.