Swan Lake Beta2 is here.
import ballerina/io;

type Employee record {
    readonly string name;
    int salary;
};

// Creates a `table` with `Employee` type members, where each
// member is uniquely identified using their `name` field.
table<Employee> key(name) t = table [
    { name: "John", salary: 100 },
    { name: "Jane", salary: 200 }
];

function increaseSalary(int n) {
    // Iterates over the rows of `t` in the specified order.
    foreach Employee e in t {
        e.salary += n;
    }

}

public function main() {
    // Retrieves `Employee` with key value `Fred`.
    Employee? e = t["Fred"];

    increaseSalary(100);
    io:println(t);
}

Table Syntax

A table is a collection of records. Each record represents a row of the table. A table is plain data if and only if its rows are plain data. A table maintains an invariant that each row is uniquely identified by a key. Each row’s key is stored in fields, which must be immutable.

Compared to maps,

  • key is part of the value rather than being separate.
  • The type of the key is not restricted to string.
  • The order of the members is preserved.


A record field can be declared as readonly. A value cannot be assigned to such a field after the record is created. The table type gives the type of the row and the name of the key field. The table constructor expression looks like an array constructor. The foreach statement will iterate over a table’s rows in their order. Use t[k] to access a row using its key.

import ballerina/io;
type Employee record {
    readonly string name;
    int salary;
};
table<Employee> key(name) t = table [
    { name: "John", salary: 100 },
    { name: "Jane", salary: 200 }
];

Creates a table with Employee type members, where each member is uniquely identified using their name field.

function increaseSalary(int n) {
    foreach Employee e in t {
        e.salary += n;
    }

Iterates over the rows of t in the specified order.

}
public function main() {
    Employee? e = t["Fred"];

Retrieves Employee with key value Fred.

    increaseSalary(100);
    io:println(t);
}
bal run table_syntax.bal
[{"name":"John","salary":200},{"name":"Jane","salary":300}]
import ballerina/io;

type Employee record {
    readonly string name;
    int salary;
};

// Creates a `table` with `Employee` type members, where each
// member is uniquely identified using their `name` field.
table<Employee> key(name) t = table [
    { name: "John", salary: 100 },
    { name: "Jane", salary: 200 }
];

function increaseSalary(int n) {
    // Iterates over the rows of `t` in the specified order.
    foreach Employee e in t {
        e.salary += n;
    }

}

public function main() {
    // Retrieves `Employee` with key value `Fred`.
    Employee? e = t["Fred"];

    increaseSalary(100);
    io:println(t);
}

Table Syntax

A table is a collection of records. Each record represents a row of the table. A table is plain data if and only if its rows are plain data. A table maintains an invariant that each row is uniquely identified by a key. Each row’s key is stored in fields, which must be immutable.

Compared to maps,

  • key is part of the value rather than being separate.
  • The type of the key is not restricted to string.
  • The order of the members is preserved.


A record field can be declared as readonly. A value cannot be assigned to such a field after the record is created. The table type gives the type of the row and the name of the key field. The table constructor expression looks like an array constructor. The foreach statement will iterate over a table’s rows in their order. Use t[k] to access a row using its key.

import ballerina/io;
type Employee record {
    readonly string name;
    int salary;
};
table<Employee> key(name) t = table [
    { name: "John", salary: 100 },
    { name: "Jane", salary: 200 }
];

Creates a table with Employee type members, where each member is uniquely identified using their name field.

function increaseSalary(int n) {
    foreach Employee e in t {
        e.salary += n;
    }

Iterates over the rows of t in the specified order.

}
public function main() {
    Employee? e = t["Fred"];

Retrieves Employee with key value Fred.

    increaseSalary(100);
    io:println(t);
}
bal run table_syntax.bal
[{"name":"John","salary":200},{"name":"Jane","salary":300}]

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand