A dynamically-typed, garbage-collected scripting language.

Version 0.10.11


This tutorial demonstrates Pyro's support for indexing by adding indexing support to a user-defined type.

You can add indexing support to a user-defined type by implementing $get() and/or $set() methods — these methods let us index into instances of the type using [] syntax.

As an example, let's build a simple ListMap type that maps keys to lists of values. If we try to look up a key that isn't in the map, we'll get back an empty list.

The Code

Here's the full code for our ListMap:

class ListMap {
    var data;

    def $init() { = {};

    def $get(key) {
        if key in {
        return [];

    def $set(key, value) {
        if key in {
        }[key] = [value];


We can now read from and write to instances of our custom ListMap type using [], just like the builtin map and vec types.

If we try to look up a key that isn't in the map, we get back an empty vec, e.g.

var map = ListMap();

assert $is_vec(map["foo"]);
assert map["foo"]:is_empty();

If we write a new key to the map, the value is added to a new list, e.g.

map["foo"] = 123;

assert $is_vec(map["foo"]);
assert map["foo"]:count() == 1;
assert map["foo"][0] == 123;

If we write more values to the map using the same key, the values are appended to the list, e.g.

map["foo"] = 456;

assert $is_vec(map["foo"]);
assert map["foo"]:count() == 2;
assert map["foo"][0] == 123;
assert map["foo"][1] == 456;