thp-web/md/learn/collections/maps.md

1.9 KiB

Maps

Also known as Associative Arrays, or Objects in other languages.

All maps must have a definition, which define their fields and datatypes. There can also be anonymous maps, which may contain any key.

Named Maps

// Here we define a map, called Person
map Person {
    String name,
    String surname,
    Int age,
}

// Here we declare an instance of a Person.
val john_doe = Person {
    name: "John",
    surname: "Doe",
    age: 33,
}

// If the compiler can infer the type of a Map,
// we can omit its type
var Person mary_jane = .{
    name: "Mary",
    surname: "Jane",
    age: 27,
}

To access the fields of a map we use square braces [].

mary_jane[age] += 1
print(mary_jane[name])  // Mary

Anonymous maps

An anonymous map allows us to store and retrieve any key of any datatype. They are declared as Map.

val car = Map {
    brand: "Toyota",
    model: "Corolla",
    year: 2012,
}

Anonymous maps can also can have their type omitted.

var car = .{
    brand: "Toyota",
    model: "Corolla",
    year: 2012,
}

If the compiler encounters a map without a type (that is, .{}) and doesn't expect a specific type, it will assume it is an anonymous map.

We can freely assign fields to an anonymous map:

// Modify an existing field
car[year] = 2015
// Create a new field
car[status] = "used"

However, if we try to access a field of an anonymous map we'll get an error.

print(car[status])  // Error: Can't access a field of an anonymous map

Instead, we should use the get function of the map, which expects a datatype and returns an Option

//                      | this function
String? car_status = car.get[String]("status")

The get function will check that a key "status" exists in the map, and that it has the correct datatype. If either the key doesn't exist or it has a different datatype, it will return None.