Types & Variables

Understanding Strada's type system and variable declarations.

Type System Overview

Strada is strongly typed. Every variable has a declared type that is checked at compile time.

Type Description Example
int64-bit signed integer42, -17
num64-bit floating point3.14, -0.5
strUTF-8 string"hello"
arrayDynamic array[1, 2, 3]
hashKey-value map{"a" => 1}
scalarGeneric type (any value)Any value
voidNo value (function returns)-
undefUndefined valueundef

Sigils

Like Perl, Strada uses sigils to identify variable types:

$ - Scalar

Single values: integers, floats, strings, references, objects

@ - Array

Ordered lists of values

% - Hash

Key-value maps (associative arrays)

Variable Declaration

Variables are declared with my followed by type and sigil:

# Scalar variables ($ sigil)
my int $count = 0;
my num $price = 19.99;
my str $name = "Alice";
my scalar $anything = 42;

# Array variables (@ sigil)
my array @numbers = [1, 2, 3];
my array @empty = [];

# Hash variables (% sigil)
my hash %config = {
    "host" => "localhost",
    "port" => 8080
};
Declaration without initialization Variables can be declared without an initial value. They will be undef until assigned.
my int $x;  # $x is undef
$x = 10;     # Now $x is 10

Integers (int)

my int $a = 42;
my int $b = -17;
my int $hex = 0xFF;      # Hexadecimal: 255
my int $oct = 0o755;     # Octal: 493
my int $bin = 0b1010;    # Binary: 10

# Arithmetic
my int $sum = $a + $b;
my int $mod = $a % 5;     # Modulo: 2
my int $pow = 2 ** 10;   # Power: 1024

Floating Point (num)

my num $pi = 3.14159;
my num $sci = 1.5e10;    # Scientific notation
my num $neg = -0.001;

# Math operations
my num $sqrt = math::sqrt(16);     # 4.0
my num $sin = math::sin($pi);     # ~0
my num $floor = math::floor(3.7); # 3

Strings (str)

my str $s1 = "Hello";
my str $s2 = 'Single quotes also work';

# Escape sequences (in double quotes)
my str $newline = "Line 1\nLine 2";
my str $tab = "Col1\tCol2";
my str $quote = "She said \"Hi\"";

# Concatenation
my str $full = $s1 . " World";

# Repetition
my str $repeated = "ab" x 3;  # "ababab"

# String functions
my int $len = strlen($s1);           # 5
my str $upper = uc($s1);            # "HELLO"
my str $lower = lc($s1);            # "hello"
my str $sub = substr($s1, 0, 3);    # "Hel"
my int $pos = index($s1, "ll");    # 2

Arrays (array)

# Declaration
my array @nums = [1, 2, 3, 4, 5];
my array @mixed = [1, "two", 3.0];
my array @nested = [[1, 2], [3, 4]];

# Pre-allocate capacity for performance
my array @big[1000];  # Empty array with capacity for 1000 elements

# Access elements (use $ sigil for single element)
my int $first = $nums[0];      # 1
my int $last = $nums[-1];     # 5 (negative index)

# Modify elements
$nums[0] = 10;

# Array functions
push(@nums, 6);                 # Add to end
my scalar $popped = pop(@nums);  # Remove from end
unshift(@nums, 0);              # Add to beginning
my scalar $shifted = shift(@nums); # Remove from beginning

my int $length = len(@nums);    # Get length
my str $joined = join(",", @nums); # Join to string
my array @sorted = sort(@nums);  # Sort array
reverse(@nums);                  # Reverse in place
Array Access Sigil When accessing a single element, use $array[index], not @array[index]. The @ sigil is for the whole array.

Hashes (hash)

# Declaration
my hash %person = {
    "name" => "Alice",
    "age" => 30,
    "city" => "NYC"
};

# Pre-allocate capacity for performance
my hash %cache[500];  # Empty hash with capacity for ~500 entries

# Access values (use $ sigil for single value)
my str $name = $person{"name"};    # "Alice"

# Add/modify values
$person{"email"} = "alice@example.com";
$person{"age"} = 31;

# Hash functions
my array @keys = keys(%person);        # Get all keys
my array @vals = values(%person);      # Get all values

# Check if key exists
if (exists(%person, "email")) {
    say("Has email");
}

# Delete a key
delete(%person, "city");
Hash Access Syntax Use $hash{key} for element access, NOT %hash{key}. Using %hash{key} is a syntax error.

References

References are created with the backslash operator \:

# References to variables
my int $x = 42;
my scalar $ref = \$x;     # Reference to scalar

my array @arr = [1, 2, 3];
my scalar $aref = \@arr;   # Reference to array

my hash %h = {"a" => 1};
my scalar $href = \%h;    # Reference to hash

# Dereference with arrow
say($aref->[0]);      # 1
say($href->{"a"});    # 1

# Anonymous references
my scalar $anon_arr = [1, 2, 3];    # Array reference
my scalar $anon_hash = {"x" => 1};  # Hash reference

Type Conversion

# String to int
my int $n = int("42");

# Int to string (automatic via concatenation)
my str $s = "" . 42;

# Explicit string conversion
my str $str = stringify(42);

The scalar Type

The scalar type can hold any value and is useful for generic programming:

func print_anything(scalar $val) void {
    say($val);
}

print_anything(42);
print_anything("hello");
print_anything([1, 2, 3]);