Skip to content

Instantly share code, notes, and snippets.

@ruvi-d
Last active January 22, 2021 08:02
Show Gist options
  • Save ruvi-d/18c10fe382dc923fc3e0ebc32a6ee738 to your computer and use it in GitHub Desktop.
Save ruvi-d/18c10fe382dc923fc3e0ebc32a6ee738 to your computer and use it in GitHub Desktop.
Ballerina design

Key features

  • 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)

New Specs

  • 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

Constructors

  • 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

  • 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

Other functions

  • .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

Value/Type operations

  • Equality checking; support == and ===
  • .clone() support
  • Immutability with .cloneReadOnly()

BIR analysis (v1.2)

  • Basics
public function main() {
    map<int> m = {
        val1:1,
        val2:2
    };
}
%2 = NewMap map<int>;
%4 = ConstLoad 1;
%6 = ConstLoad first;
%2[%6] = %4;
  • Same syntax for Records
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 pointers
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;
  • For errors
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;
  • Member access
public function main() {
    map<int> m = {
        val1:1,
        val2:2
    };
    var val = m["val1"];
}
%13 = ConstLoad val1;
%11 = %1[%13];
  • Get
public function main() {
    map<int> m = {
        val1:1,
        val2:2
    };
    var val = m.get("val1");
}
%14 = ConstLoad val1;
%15 = get(%1, %14);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment