Swan Lake Beta3 is here.
import ballerina/io;

// A `const` is immutable.
const s = "Anne";

type Row record {
    // Both the field and its value are immutable.
    readonly string k;
    int value;

};

table<Row> key(k) t = table [
    { k: "John", value: 17 }
];

public function main() {
    // Can safely use `s` as a key.
    t.add({k: s, value: 18});

    io:println(t);
}

Readonly Type

The readonly type consists of values that are immutable. For structural type T, T & readonly means immutable T. T & readonly is subtype of T and subtype of readonly. Guaranteed that if declared type of a value is a subtype of readonly, then at runtime the value can never be mutated. It is enforced by runtime checks on the mutating structures. With readonly field, both the field and its value are immutable.

import ballerina/io;
const s = "Anne";

A const is immutable.

type Row record {
    readonly string k;
    int value;

Both the field and its value are immutable.

};
table<Row> key(k) t = table [
    { k: "John", value: 17 }
];
public function main() {
    t.add({k: s, value: 18});

Can safely use s as a key.

    io:println(t);
}
bal run readonly_type.bal
[{"k":"John","value":17},{"k":"Anne","value":18}]
import ballerina/io;

// A `const` is immutable.
const s = "Anne";

type Row record {
    // Both the field and its value are immutable.
    readonly string k;
    int value;

};

table<Row> key(k) t = table [
    { k: "John", value: 17 }
];

public function main() {
    // Can safely use `s` as a key.
    t.add({k: s, value: 18});

    io:println(t);
}

Readonly Type

The readonly type consists of values that are immutable. For structural type T, T & readonly means immutable T. T & readonly is subtype of T and subtype of readonly. Guaranteed that if declared type of a value is a subtype of readonly, then at runtime the value can never be mutated. It is enforced by runtime checks on the mutating structures. With readonly field, both the field and its value are immutable.

import ballerina/io;
const s = "Anne";

A const is immutable.

type Row record {
    readonly string k;
    int value;

Both the field and its value are immutable.

};
table<Row> key(k) t = table [
    { k: "John", value: 17 }
];
public function main() {
    t.add({k: s, value: 18});

Can safely use s as a key.

    io:println(t);
}
bal run readonly_type.bal
[{"k":"John","value":17},{"k":"Anne","value":18}]
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