Swan Lake Beta2 is here.
import ballerina/io;

// Defines a record type named `Coord`.
type Coord record {
    int x;
    int y;
};

public function main() {
    // Creates a `record`, specifying values for its fields.
    record { int x; int y; } r = {
        x: 1,
        y: 2
    };

    // Creates a `Coord` record.
    Coord c = {
        x: 1,
        y: 2
    };

    int a = r.y;
    io:println(a);

    int b = c.x;
    io:println(b);
}

Records

A record type has specific named fields. Fields can be accessed with r.x. Records are mutable: r.x is an lvalue. Records can be constructed using a similar syntax to a map. Typically a record type is combined with a type definition. The name of the type is not significant: a record is just a collection of fields. Record equality works same as map equality.

import ballerina/io;
type Coord record {
    int x;
    int y;
};

Defines a record type named Coord.

public function main() {
    record { int x; int y; } r = {
        x: 1,
        y: 2
    };

Creates a record, specifying values for its fields.

    Coord c = {
        x: 1,
        y: 2
    };

Creates a Coord record.

    int a = r.y;
    io:println(a);
    int b = c.x;
    io:println(b);
}
bal run records.bal
2
1
import ballerina/io;

// Defines a record type named `Coord`.
type Coord record {
    int x;
    int y;
};

public function main() {
    // Creates a `record`, specifying values for its fields.
    record { int x; int y; } r = {
        x: 1,
        y: 2
    };

    // Creates a `Coord` record.
    Coord c = {
        x: 1,
        y: 2
    };

    int a = r.y;
    io:println(a);

    int b = c.x;
    io:println(b);
}

Records

A record type has specific named fields. Fields can be accessed with r.x. Records are mutable: r.x is an lvalue. Records can be constructed using a similar syntax to a map. Typically a record type is combined with a type definition. The name of the type is not significant: a record is just a collection of fields. Record equality works same as map equality.

import ballerina/io;
type Coord record {
    int x;
    int y;
};

Defines a record type named Coord.

public function main() {
    record { int x; int y; } r = {
        x: 1,
        y: 2
    };

Creates a record, specifying values for its fields.

    Coord c = {
        x: 1,
        y: 2
    };

Creates a Coord record.

    int a = r.y;
    io:println(a);
    int b = c.x;
    io:println(b);
}
bal run records.bal
2
1

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