Skip to main content
Variables in OCat are explicitly typed. Every declaration specifies the type of the value it holds, followed by a name and an initial value. Variables are stored in the current execution context and are accessible to any code that runs in the same context, including functions that are called later.

Declaring a variable

The declaration syntax is:
<type> <name> = <literal>
All three parts — type, name, and initial value — are required. There is no concept of an uninitialized variable.
number score = 100
string player = "Alice"
bool active = true

Types

OCat has three value types:
Stores an integer or floating-point number. The literal pattern is one or more digits, optionally followed by a decimal point and more digits.
number count = 0
number price = 9.99
number year = 2024
DetailValue
Keywordnumber
Literal pattern\d+(\.\d+)?
Example literals0, 42, 3.14
Stores a sequence of characters enclosed in double quotes. Escape sequences using a backslash are supported within the string literal.
string name = "Alice"
string message = "Hello, world!"
string path = "src/main"
String values also support interpolation using ${varName} syntax:
string name = "Alice"
string greeting = "Hello, ${name}!"
print(greeting)
DetailValue
Keywordstring
Literal pattern"..." (double-quoted)
Interpolation${identifier} inside the string
Stores a boolean value. The only valid literals are true and false.
bool enabled = true
bool debug = false
DetailValue
Keywordbool
Valid literalstrue, false

Mutating a variable with set

The set keyword is part of the OCat syntax for updating a variable’s value. The full declaration syntax (type, name, and value) is repeated with set as a prefix:
number x = 10
set number x = 20
string status = "pending"
set string status = "complete"

bool active = false
set bool active = true
In the current version (1.3.x), the re-declaration enforcement logic for set is not yet active in the runner. Both a plain declaration and a set declaration write to the same variable slot. The set keyword is parsed correctly but does not currently enforce that the variable was previously declared.

Constants with const

The const keyword is defined in the lexer and accepted by the parser. The syntax is:
const number MAX_SIZE = 1024
const string APP_NAME = "OCat"
In the current version (1.3.x), const is parsed but not enforced at runtime — the isConst property in the AST is always set to false by the variable builder. Reassigning a const variable does not raise an error yet. This behavior is planned for a future release.

Printing variables

Pass the variable name (without quotes) to print to output its current value:
number score = 42
print(score)
You can also print a string literal that references a variable through interpolation:
string name = "Alice"
print("Hello, ${name}!")

Scope

Variables are stored in the CoreContext that is active when the declaration runs. Variables declared at the top level of a file are available throughout that file. Variables declared inside a function body are stored in that function’s own isolated scope and do not affect the outer context.
number global = 1

func example() {
    number local = 2
    print(local)
}

call example()
print(global)
// print(local)  — this would be an undeclared variable error

Types

Detailed reference for each value type and its literal syntax.

Functions

How function scope relates to variable visibility.

Build docs developers (and LLMs) love