Swan Lake Beta2 is here.
import ballerina/io;

// `Person` type allows additional fields with `anydata` values.
type Person record {
    string name;
};

// `Employee` type allows additional fields with `anydata` values.
type Employee record {
    string name;
    int id;
};

// Adds an additional `id` field to `e`.
Employee e = {
    name: "James", id: 10
};

// You can assign an `Employee` type value to a `Person`.
Person p = e;

Person p2 = {
    name: "John", "country": "UK"
};

// You can assign a `Person` type value to a `map`.
map<anydata> m = p2;

public function main() {
    io:println(p);
    io:println(m);
}

Open Records

Record types are by default open: they allow fields other than those specified. The type of unspecified fields are anydata. Records are maps. Open records belongs to map<anydata>. Use quoted keys for fields not mentioned in the record type.

import ballerina/io;
type Person record {
    string name;
};

Person type allows additional fields with anydata values.

type Employee record {
    string name;
    int id;
};

Employee type allows additional fields with anydata values.

Employee e = {
    name: "James", id: 10
};

Adds an additional id field to e.

Person p = e;

You can assign an Employee type value to a Person.

Person p2 = {
    name: "John", "country": "UK"
};
map<anydata> m = p2;

You can assign a Person type value to a map.

public function main() {
    io:println(p);
    io:println(m);
}
bal run open_records.bal
{"name":"James","id":10}
{"name":"John","country":"UK"}
import ballerina/io;

// `Person` type allows additional fields with `anydata` values.
type Person record {
    string name;
};

// `Employee` type allows additional fields with `anydata` values.
type Employee record {
    string name;
    int id;
};

// Adds an additional `id` field to `e`.
Employee e = {
    name: "James", id: 10
};

// You can assign an `Employee` type value to a `Person`.
Person p = e;

Person p2 = {
    name: "John", "country": "UK"
};

// You can assign a `Person` type value to a `map`.
map<anydata> m = p2;

public function main() {
    io:println(p);
    io:println(m);
}

Open Records

Record types are by default open: they allow fields other than those specified. The type of unspecified fields are anydata. Records are maps. Open records belongs to map<anydata>. Use quoted keys for fields not mentioned in the record type.

import ballerina/io;
type Person record {
    string name;
};

Person type allows additional fields with anydata values.

type Employee record {
    string name;
    int id;
};

Employee type allows additional fields with anydata values.

Employee e = {
    name: "James", id: 10
};

Adds an additional id field to e.

Person p = e;

You can assign an Employee type value to a Person.

Person p2 = {
    name: "John", "country": "UK"
};
map<anydata> m = p2;

You can assign a Person type value to a map.

public function main() {
    io:println(p);
    io:println(m);
}
bal run open_records.bal
{"name":"James","id":10}
{"name":"John","country":"UK"}

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