Swan Lake Beta3 is here.
import ballerina/io;

type Employee record {
    readonly string firstName;
    readonly string lastName;
    int salary;
};

public function main() {
    // `t` has a key sequence with `firstName` and `lastName` fields.
    table<Employee> key(firstName, lastName) t = table [
        {firstName: "John", lastName: "Smith", salary: 100},
        {firstName: "Fred", lastName: "Bloggs", salary: 200}
    ];

    // The key sequence provides keyed access to members of the `table`.
    Employee? e = t["Fred", "Bloggs"];


    io:println(e);
}

Multiple Key Fields

A table provides access to its members using a key that comes from the read-only fields of the member. It is a key sequence, which is used to provide keyed access to its members. The key sequence is an ordered sequence of field names.

import ballerina/io;
type Employee record {
    readonly string firstName;
    readonly string lastName;
    int salary;
};
public function main() {
    table<Employee> key(firstName, lastName) t = table [
        {firstName: "John", lastName: "Smith", salary: 100},
        {firstName: "Fred", lastName: "Bloggs", salary: 200}
    ];

t has a key sequence with firstName and lastName fields.

    Employee? e = t["Fred", "Bloggs"];

The key sequence provides keyed access to members of the table.

    io:println(e);
}
bal run multiple_key_fields.bal
{"firstName":"Fred","lastName":"Bloggs","salary":200}
import ballerina/io;

type Employee record {
    readonly string firstName;
    readonly string lastName;
    int salary;
};

public function main() {
    // `t` has a key sequence with `firstName` and `lastName` fields.
    table<Employee> key(firstName, lastName) t = table [
        {firstName: "John", lastName: "Smith", salary: 100},
        {firstName: "Fred", lastName: "Bloggs", salary: 200}
    ];

    // The key sequence provides keyed access to members of the `table`.
    Employee? e = t["Fred", "Bloggs"];


    io:println(e);
}

Multiple Key Fields

A table provides access to its members using a key that comes from the read-only fields of the member. It is a key sequence, which is used to provide keyed access to its members. The key sequence is an ordered sequence of field names.

import ballerina/io;
type Employee record {
    readonly string firstName;
    readonly string lastName;
    int salary;
};
public function main() {
    table<Employee> key(firstName, lastName) t = table [
        {firstName: "John", lastName: "Smith", salary: 100},
        {firstName: "Fred", lastName: "Bloggs", salary: 200}
    ];

t has a key sequence with firstName and lastName fields.

    Employee? e = t["Fred", "Bloggs"];

The key sequence provides keyed access to members of the table.

    io:println(e);
}
bal run multiple_key_fields.bal
{"firstName":"Fred","lastName":"Bloggs","salary":200}
Subscribe to the newsletter

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