Skip to content

Variables and Types

Types

merx has three types:

TypeDescriptionExamples
int64-bit signed integer0, 42, -17
strUTF-8 string'hello', ''
boolBooleantrue, false

There are no implicit type conversions. To convert between types, use the as operator (see Type Casting).

String Literals

Strings are enclosed in single quotes:

'Hello, World!'
'merx'
''

Escape Sequences

The following escape sequences are available inside string literals:

SequenceMeaning
\\'Single quote
\\\\Backslash
\\nNewline (LF)
\\tTab
\\rCarriage return (CR)
\\0Null character
\\xHHHex byte value

A bare \ or ' inside a string is not allowed; you must use the escape sequence.

Example:

mermaid
flowchart TD
    Start --> A[println 'line1\\nline2']
    A --> B[println 'it\\'s merx']
    B --> End
console
$ merx run escape.mmd
line1
line2
it's merx

Variables

Assignment

Use = to assign a value to a variable:

mermaid
flowchart TD
    Start --> A[x = 42]
    A --> B[println x]
    B --> End
console
$ merx run assign.mmd
42

Dynamic Typing

Variables can hold values of any type, and the type can change on reassignment:

mermaid
flowchart TD
    Start --> A[x = 42; println x]
    A --> B[x = 'hello'; println x]
    B --> End
console
$ merx run dynamic.mmd
42
hello

Global Scope

All variables are global. A variable assigned in one node is accessible from any subsequent node:

mermaid
flowchart TD
    Start --> A[name = 'merx']
    A --> B[println 'Hello, ' + name + '!']
    B --> End
console
$ merx run scope.mmd
Hello, merx!

Referencing an undefined variable causes a runtime error.

Multiple Statements

You can write multiple statements in a single Process node by separating them with semicolons ;:

mermaid
flowchart TD
    Start --> A[x = 1; y = 2; println x + y]
    A --> End
console
$ merx run multi.mmd
3

Statements are executed in order from left to right.

Type Casting

The as operator converts a value to a different type:

expression as type
FromToBehavior
intintNo-op
strintParses as decimal. Runtime error on failure
boolintError (not supported)
intstrConverts to decimal string
strstrNo-op
boolstr'true' or 'false'

There is no bool cast target in the language.

mermaid
flowchart TD
    Start --> A[x = 42 as str]
    A --> B[println 'The answer is ' + x]
    B --> C[y = '123' as int]
    C --> D[println y + 1]
    D --> End
console
$ merx run cast.mmd
The answer is 42
124