Skip to content

list, dict, str

These three namespaces declare methods — procedures whose first parameter is named self. Both call forms type-check, but only one builds:

scores.contains(4); # method form ✅
list.contains(scores, 4); # namespace form ⛔ "undeclared list 'list'"

Use the method form everywhere.

Language-level syntax for these types is in Lists and dicts; this page is the API surface.

scores.add(item: T) -> void ✅ # data_addtolist
scores.contains(item: T) -> bool ✅ # data_listcontainsitem
scores.length() -> num ✅ # data_lengthoflist
scores.clear() -> void ✅ # data_deletealloflist
scores.indexOf(item: T) -> num ✅ # data_itemnumoflist
scores.show() -> void ✅ # show the list monitor
scores.hide() -> void ✅
scores.insert(item: T, idx: num) ✅ # data_insertatlist
scores.remove(idx: num) -> T ✅ # reads the item, then deletes it
scores.merge(addition: list<T>) ✅ # appends every item
public scores: list<num> = [3, 1, 4];
proc tally() -> void {
scores.add(7);
private n: num = scores.length();
private where: num = scores.indexOf(4); # 0 if absent
if (scores.contains(1)) { ... }
}

T binds from the receiver, so scores.contains("x") on a list<num> is a type error.

scores.insert(9, 1); # 9 becomes the first item
private gone: num = scores.remove(1); # delete item 1 and keep its value
scores.remove(1); # a discarded result emits only the delete
scores.merge(other);
scores.merge(scores); # safe: the count is read once, up front

merge takes a declared list or a slice, not a literal. On a list<Struct>, insert and remove work and merge does not; see Struct lists.

List slices lower through a stdlib procedure, sliceInto, which cannot be called directly. Write dst = xs[start:stop:step]; instead.

stock.contains(key: K) -> bool ✅
stock.length() -> num ✅
stock.keys() -> list<K> ✅ # live and read-only
stock.values() -> list<V> ✅ # live and read-only
stock.merge(addition) -> void ✅ # shared keys take the new value

keys() and values() read the dict’s column directly, so writing to one is a build error: it would put the columns out of step. Store one in a list to get a copy you can change:

for (name, stock.keys()) { looks.say(name, 1); }
private names: list<str> = stock.keys(); # a copy
names.add("extra");

merge only takes a declared dict, not a literal.

The rest of a dict is language syntax rather than a method:

public stock: dict<str, num> = {"apple": 2, "banana": 5};
private n: num = stock["apple"]; # ✅ read
stock["cherry"] = 7; # ✅ write — appends if the key is new
stock["apple"] += 1; # ✅ compound assignment
for ((name, count), stock) { # ✅ iterate both columns
report(name, count);
}
greeting.contains(substring: str) -> bool ✅ # operator_contains
greeting.slice(start: num, stop: num, step: num = 1) -> str ✅ # what s[a:b:c] calls

That is the whole namespace so far. An any value has these methods too, so answer.contains("y") type-checks without a Str() cast.

Other string operations live elsewhere:

len(greeting); # prelude — operator_length
greeting[1]; # 1-based indexing — operator_letter_of
greeting + name; # operator_join
f"{greeting} {name}"; # interpolation, also join
for (letter, greeting) { ... }

There is no split, replace, toUpper, or trim. Slices cover substrings, see Slices. Build the rest by walking characters:

proc countChar(s: str, target: str) -> num {
private n: num = 0;
for (c, s) {
if (c == target) { n += 1; }
}
return n;
}