Skip to content

Constants and variables

tim-hardcastle edited this page Jul 30, 2024 · 6 revisions

Constants can be defined under the heading const. The Pipefish style guide recommends that you use SCREAMING_SNAKE_CASE to name them.

Variables can be declared under the heading var.

An examples is given in examples/variables.pf

var

h = "Hello world!"
x = 2 + 2

const

MONTHS_IN_A_YEAR = 12

You can change the values of variables via the REPL, but unlike e.g. Python, you can't create variables in the REPL: they must be declared in the script.

In order to interact with variables in a command, you must bring them into the scope of the command using the global keyword. E.g if we add this to the script above, it will do what you think it would do.

cmd

addToXAndShow(n) : 
    global x
    x = x + n
    post x

In Pipefish, variables by default take on the type of the thing assigned to them on declaration. So x is of type int, and h is of type string, and trying to store anything else in them causes an error.

This is only the default behavior — Pipefish is after all a dynamic language. We'll come back to this when we look at the type system.

Clone this wiki locally