- Mutable collection of members that have a key (always strings)
- Field = members + it's key
- Name of the field is the key
- Value of the field is the value of the member
- Keys are unique (i.e. two members can't have the same key)
- Members can be
any
(including function pointers) | error
type
- A reference type (to === fails for cloned)
- Iterable
- Preferred to iterate in order they were added (unordered)
- Each field has a read-only bit; in addition to the read-only bit of the map value
& readonly
- If field is read-only then it can't be assigned to nor removed
- If map value is read-only then automatically all fields are also read-only
- Using a record literal
{string_key:member, ..}
- Can include variable reference
{variable}
- Key can be computed
{ [expression that returns string]:value}
- A spread field expression can also be use to include another map/record
- Access operator
[key]
; returns member_type|()
.entries()
; return a map with [key, member] for each key
.iterator()
; iterate the members (not keys)
.get(key)
; returns member_type or throws exception
.haskey(key)
.remove(key)
, .removeAll()
and .removeIfHasKey(key)
.toArray()
; returns members as array
.filter(func)
; selects members from a map
.forEach(func)
; applies function to each member
.keys()
.length()
.map(func)
; like forEach() but returns new map
.reduce(func,T)
; combines all the members to a new value
- Equality checking; support
==
and ===
.clone()
support
- Immutability with
.cloneReadOnly()
public function main() {
map<int> m = {
val1:1,
val2:2
};
}
%2 = NewMap map<int>;
%4 = ConstLoad 1;
%6 = ConstLoad first;
%2[%6] = %4;
type Student record {
string name;
int age;
};
public function main() {
Student jim = {name: "first", age: 1};
Student jake = {name: "second", age: 2};
map<Student> m = {
jim,
jake
};
}
%2(LOCAL) Student;
%2 = NewMap Student;
%4 = ConstLoad first;
%6 = ConstLoad name;
%2[%6] = %4;
function test(int x) {
return;
}
function test2(int x) {
return;
}
public function main() {
map<function (int)> m = {
test,
test2
};
}
%2(LOCAL) map<function(int) -> ()>;
%2 = NewMap map<function(int) -> ()>;
%4 = fp $anon/.:0.0.0::test;
%6 = ConstLoad test;
%2[%6] = %4;
public function main() {
error simpleError = error("SimpleErrorType", message = "Simple error occurred");
error simpleError2 = error("SimpleErrorType2", message = "Simple error2 occurred");
map<error> m = {
simpleError,
simpleError2
};
}
%28(LOCAL) map<error>;
%17 = <$anonType$builtin$0> %26;
%14 = error error(%16, %17);
%28 = NewMap map<error>;
%32 = ConstLoad simpleError;
%28[%32] = %1;
%35 = ConstLoad simpleError2;
%28[%35] = %14;
public function main() {
map<int> m = {
val1:1,
val2:2
};
var val = m["val1"];
}
%13 = ConstLoad val1;
%11 = %1[%13];
public function main() {
map<int> m = {
val1:1,
val2:2
};
var val = m.get("val1");
}
%14 = ConstLoad val1;
%15 = get(%1, %14);