Swan Lake Beta2 is here.
import ballerina/io;

type Coord record {|
    float x;
    float y;
|};

Coord x = { x: 1.0, y: 2.0 };

// `x` is a `map` with `float` values.
map<float> m1 = x;

type Headers record {|
    string 'from;
    string to;
    string...;
|};

Headers h = {
    'from: "Jane", to: "John"
};

// `h` is a `map` with `string` values.
map<string> m2 = h;

public function main() {
    io:println(m1);
    io:println(m2);
}

Controlling Openness

Use record {| ... |} to describe a record type that allows exclusively what is specified in the body. Use T... to allow other fields of type T. map<T> is same as record {| T...; |}.

import ballerina/io;
type Coord record {|
    float x;
    float y;
|};
Coord x = { x: 1.0, y: 2.0 };
map<float> m1 = x;

x is a map with float values.

type Headers record {|
    string 'from;
    string to;
    string...;
|};
Headers h = {
    'from: "Jane", to: "John"
};
map<string> m2 = h;

h is a map with string values.

public function main() {
    io:println(m1);
    io:println(m2);
}
bal run controlling_openness.bal
{"x":1.0,"y":2.0}
{"from":"Jane","to":"John"}
import ballerina/io;

type Coord record {|
    float x;
    float y;
|};

Coord x = { x: 1.0, y: 2.0 };

// `x` is a `map` with `float` values.
map<float> m1 = x;

type Headers record {|
    string 'from;
    string to;
    string...;
|};

Headers h = {
    'from: "Jane", to: "John"
};

// `h` is a `map` with `string` values.
map<string> m2 = h;

public function main() {
    io:println(m1);
    io:println(m2);
}

Controlling Openness

Use record {| ... |} to describe a record type that allows exclusively what is specified in the body. Use T... to allow other fields of type T. map<T> is same as record {| T...; |}.

import ballerina/io;
type Coord record {|
    float x;
    float y;
|};
Coord x = { x: 1.0, y: 2.0 };
map<float> m1 = x;

x is a map with float values.

type Headers record {|
    string 'from;
    string to;
    string...;
|};
Headers h = {
    'from: "Jane", to: "John"
};
map<string> m2 = h;

h is a map with string values.

public function main() {
    io:println(m1);
    io:println(m2);
}
bal run controlling_openness.bal
{"x":1.0,"y":2.0}
{"from":"Jane","to":"John"}

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