Array

Fixed-size typed arrays. Size is set at declaration and does not change. Use List for dynamic resizable collections.


Declaration

Array:New name[size]:Type;

size can be a literal or a variable expression. Type is Number, String, or Boolean.

Array:New scores[5]:Number;
Array:New names[3]:String;
Array:New flags[4]:Boolean;

Var:Number n = 10;
Array:New buf[n]:Number;    // size from variable

Elements are zero-initialised: 0, "", or false.

Access

scores[0] = 95;
scores[1] = 87;
Konsol:Print(scores[0]);    // 95

Out-of-bounds reads and writes throw ArrayException with message = "Index out of bounds" and code set to the attempted index.

try {
    Var:Number bad = scores[99];
} catch (ArrayException e) {
    Konsol:Print(e.message);    // Index out of bounds
    Konsol:Print(e.code);       // 99
}

Iterating

for (Number i = 0; i < 5; i++) {
    Konsol:Print(scores[i]);
}

foreach (s in scores) {
    Konsol:Print(s);
}

Collections of class instances

Array:New herd[10]:Animal;   // array of Animal instances

Note on performance

Array and List currently share the same internal Value storage, so there is no runtime performance difference between them. Array is kept as a distinct type to leave room for a typed contiguous-storage optimisation in a future release.


Examples

Arrays

// 05_arrays.ks — Arrays (fixed-size, typed)

Konsol:Print("=== Arrays ===");

// Number array
Array:New scores[5]:Number;
scores[0] = 95;
scores[1] = 87;
scores[2] = 72;
scores[3] = 60;
scores[4] = 100;

Konsol:Print("--- scores ---");
for (Var:Number i = 0; i < 5; i++) {
    Konsol:Print("scores[${i}] = ${scores[i]}");
}

// String array
Array:New names[3]:String;
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Carol";

Konsol:Print("--- names ---");
for (Var:Number i = 0; i < 3; i++) {
    Konsol:Print(names[i]);
}

// Boolean array
Array:New flags[4]:Boolean;
flags[0] = true;
flags[1] = false;
flags[2] = true;
flags[3] = false;

Konsol:Print("--- flags ---");
for (Var:Number i = 0; i < 4; i++) {
    Konsol:Print("flags[${i}] = ${flags[i]}");
}

// Dynamic size from variable
Var:Number size = 6;
Array:New evens[size]:Number;
for (Var:Number i = 0; i < size; i++) {
    evens[i] = i * 2;
}
Konsol:Print("--- evens ---");
for (Var:Number i = 0; i < size; i++) {
    Konsol:Print(evens[i]);
}

// Accumulate sum
Var:Number total = 0;
for (Var:Number i = 0; i < 5; i++) {
    total += scores[i];
}
Konsol:Print("sum of scores = ${total}");
Konsol:Print("avg of scores = ${total / 5}");

// Array element in expression
Konsol:Print("scores[0] + scores[4] = ${scores[0] + scores[4]}");

// ── Push / Size / Pop ─────────────────────────────────────────────────────────
Konsol:Print("--- push / size / pop ---");
Array:Number dyn;
Array:Push(10, dyn);
Array:Push(20, dyn);
Array:Push(30, dyn);

Var:Number sz;
Array:Size(dyn, sz);
Konsol:Print("size after 3 pushes: ${sz}");

for (Var:Number i = 0; i < sz; i++) {
    Konsol:Print("dyn[${i}] = ${dyn[i]}");
}

Var:Number popped;
Array:Pop(dyn, popped);
Konsol:Print("popped: ${popped}");

Array:Size(dyn, sz);
Konsol:Print("size after pop: ${sz}");

// GetLength alias
Var:Number len;
Array:GetLength(dyn, len);
Konsol:Print("GetLength alias: ${len}");

// ── Initializer list ──────────────────────────────────────────────────────────
Konsol:Print("--- initializer list ---");
Array:Number primes = {2, 3, 5, 7, 11};
Var:Number pLen;
Array:Size(primes, pLen);
Konsol:Print("primes count: ${pLen}");
for (Var:Number i = 0; i < pLen; i++) {
    Konsol:Print("primes[${i}] = ${primes[i]}");
}

Array:String fruits = {"apple", "banana", "cherry"};
Var:Number fLen;
Array:Size(fruits, fLen);
Konsol:Print("fruits count: ${fLen}");
for (Var:Number i = 0; i < fLen; i++) {
    Konsol:Print(fruits[i]);
}

Array:Boolean bits = {true, false, true, true};
Var:Number bLen;
Array:Size(bits, bLen);
Konsol:Print("bits count: ${bLen}");
for (Var:Number i = 0; i < bLen; i++) {
    Konsol:Print("bits[${i}] = ${bits[i]}");
}

// ── ArrayException — out-of-bounds ───────────────────────────────────────────
Konsol:Print("--- ArrayException ---");
Array:Number oob = {1, 2, 3};

// Read beyond end
try {
    Var:Number bad = oob[5];
} catch (ArrayException e) {
    Konsol:Print("read oob caught: ${e.message}");   // Index out of bounds
    Konsol:Print("read oob index : ${e.code}");      // 5
}

// Write beyond end
try {
    oob[10] = 99;
} catch (ArrayException e) {
    Konsol:Print("write oob caught: ${e.message}");  // Index out of bounds
    Konsol:Print("write oob index : ${e.code}");     // 10
}

Array push / pop

// 35_array_push_pop.ks — Array:Push / Array:Pop

Konsol:Print("=== push onto Number array ===");
Array:Number scores;
Array:Push(scores, 10);
Array:Push(scores, 20);
Array:Push(scores, 30);
Konsol:Print(scores[0]);    // 10
Konsol:Print(scores[1]);    // 20
Konsol:Print(scores[2]);    // 30

Konsol:Print("=== pop from Number array ===");
Var:Number last;
Array:Pop(scores, last);
Konsol:Print(last);         // 30
Konsol:Print(scores[0]);    // 10
Konsol:Print(scores[1]);    // 20

Konsol:Print("=== push onto String array ===");
Array:String tags;
Array:Push(tags, "alpha");
Array:Push(tags, "beta");
Array:Push(tags, "gamma");
Konsol:Print(tags[0]);      // alpha
Konsol:Print(tags[2]);      // gamma

Konsol:Print("=== pop from String array ===");
Var:String t;
Array:Pop(tags, t);
Konsol:Print(t);            // gamma
Konsol:Print(tags[0]);      // alpha
Konsol:Print(tags[1]);      // beta

Konsol:Print("=== push onto initializer array ===");
Array:Number counts = {1, 2, 3};
Array:Push(counts, 4);
Konsol:Print(counts[3]);    // 4

Konsol:Print("=== pop all ===");
Var:Number x;
Array:Pop(counts, x); Konsol:Print(x);  // 4
Array:Pop(counts, x); Konsol:Print(x);  // 3
Array:Pop(counts, x); Konsol:Print(x);  // 2
Array:Pop(counts, x); Konsol:Print(x);  // 1

Konsol:Print("done");