Swan Lake Beta2 is here.
import ballerina/io;

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

public function main() {
    // key field, `name` is of `record` type.
    table<Employee> key(name) t = table [
        {name: {first: "John", last: "Smith"}, salary: 100},
        {name: {first: "Fred", last: "Bloggs"}, salary: 200}
    ];

    Employee? e = t[{first: "Fred", last: "Bloggs"}];
    io:println(e);
}

Structured Keys

Key fields can be structured where they belong to any subtype of plain data. The Value of key field must be immutable. Initializer of readonly field will be constructed as immutable. In other cases, can use cloneReadOnly to create an immutable value.

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

key field, name is of record type.

    Employee? e = t[{first: "Fred", last: "Bloggs"}];
    io:println(e);
}
bal run structured_keys.bal
{"name":{"first":"Fred","last":"Bloggs"},"salary":200}
import ballerina/io;

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

public function main() {
    // key field, `name` is of `record` type.
    table<Employee> key(name) t = table [
        {name: {first: "John", last: "Smith"}, salary: 100},
        {name: {first: "Fred", last: "Bloggs"}, salary: 200}
    ];

    Employee? e = t[{first: "Fred", last: "Bloggs"}];
    io:println(e);
}

Structured Keys

Key fields can be structured where they belong to any subtype of plain data. The Value of key field must be immutable. Initializer of readonly field will be constructed as immutable. In other cases, can use cloneReadOnly to create an immutable value.

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

key field, name is of record type.

    Employee? e = t[{first: "Fred", last: "Bloggs"}];
    io:println(e);
}
bal run structured_keys.bal
{"name":{"first":"Fred","last":"Bloggs"},"salary":200}

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