2023-10-02 01:41:38 +00:00
|
|
|
# Maps
|
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
Also known as Associative Arrays, or Objects in other languages.
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
All maps must have a definition, which define their fields and datatypes.
|
|
|
|
There can also be anonymous maps, which may contain any key.
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
## Named Maps
|
2023-10-02 01:41:38 +00:00
|
|
|
|
|
|
|
```thp
|
2024-03-13 17:16:29 +00:00
|
|
|
// 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 {
|
2023-10-02 01:41:38 +00:00
|
|
|
name: "John",
|
|
|
|
surname: "Doe",
|
|
|
|
age: 33,
|
|
|
|
}
|
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
```
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
To access the fields of a map we use square braces `[]`.
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
```thp
|
|
|
|
mary_jane[age] += 1
|
|
|
|
print(mary_jane[name]) // Mary
|
2023-10-02 01:41:38 +00:00
|
|
|
```
|
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
## Anonymous maps
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
An anonymous map allows us to store and retrieve any key of any datatype.
|
|
|
|
They are declared as `Map`.
|
2023-10-02 01:41:38 +00:00
|
|
|
|
|
|
|
```thp
|
2024-03-13 17:16:29 +00:00
|
|
|
val car = Map {
|
|
|
|
brand: "Toyota",
|
|
|
|
model: "Corolla",
|
|
|
|
year: 2012,
|
2023-10-02 01:41:38 +00:00
|
|
|
}
|
2024-03-13 17:16:29 +00:00
|
|
|
```
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
Anonymous maps can also can have their type omitted.
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
```thp
|
|
|
|
var car = .{
|
|
|
|
brand: "Toyota",
|
|
|
|
model: "Corolla",
|
|
|
|
year: 2012,
|
2023-10-02 01:41:38 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
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:
|
|
|
|
|
|
|
|
```thp
|
|
|
|
// Modify an existing field
|
|
|
|
car[year] = 2015
|
|
|
|
// Create a new field
|
|
|
|
car[status] = "used"
|
|
|
|
```
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
However, if we try to access a field of an anonymous map we'll get an error.
|
|
|
|
|
|
|
|
```thp
|
|
|
|
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
|
|
|
|
|
|
|
|
```thp
|
|
|
|
// | this function
|
|
|
|
String? car_status = car.get[String]("status")
|
|
|
|
```
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2024-03-13 17:16:29 +00:00
|
|
|
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`.
|
2023-10-02 01:41:38 +00:00
|
|
|
|