Swan Lake Beta3 is here.
import ballerina/io;

type Person record {
 string first;
 string last;
 int yearOfBirth;
};

public function main() {
    Person[] persons = [
        {first: "Melina", last: "Kodel", yearOfBirth: 1994},
        {first: "Tom", last: "Riddle", yearOfBirth: 1926}
    ];

    // A `Person` record is destructured here, as a
    // projection with `first` and `last` fields.
    // `{first: f, last: l}` is the `binding pattern`.
    var names1 = from var {first: f, last: l} in persons
                select {first: f, last: l};


    io:println(names1);

    // The same can be simplified as this.
    var names2 = from var {first, last} in persons
                select {first, last};


    io:println(names2);
}

Destructuring Records

Destructuring records is particularly useful with query expressions, but works anywhere you can have var. var is followed by a binding pattern. The semantics of binding pattern is open. {x} is short for {x: x} in both binding patterns and record constructors.

import ballerina/io;
type Person record {
 string first;
 string last;
 int yearOfBirth;
};
public function main() {
    Person[] persons = [
        {first: "Melina", last: "Kodel", yearOfBirth: 1994},
        {first: "Tom", last: "Riddle", yearOfBirth: 1926}
    ];
    var names1 = from var {first: f, last: l} in persons
                select {first: f, last: l};

A Person record is destructured here, as a projection with first and last fields. {first: f, last: l} is the binding pattern.

    io:println(names1);
    var names2 = from var {first, last} in persons
                select {first, last};

The same can be simplified as this.

    io:println(names2);
}
bal run destructuring_records.bal
[{"first":"Melina","last":"Kodel"},{"first":"Tom","last":"Riddle"}]
[{"first":"Melina","last":"Kodel"},{"first":"Tom","last":"Riddle"}]
import ballerina/io;

type Person record {
 string first;
 string last;
 int yearOfBirth;
};

public function main() {
    Person[] persons = [
        {first: "Melina", last: "Kodel", yearOfBirth: 1994},
        {first: "Tom", last: "Riddle", yearOfBirth: 1926}
    ];

    // A `Person` record is destructured here, as a
    // projection with `first` and `last` fields.
    // `{first: f, last: l}` is the `binding pattern`.
    var names1 = from var {first: f, last: l} in persons
                select {first: f, last: l};


    io:println(names1);

    // The same can be simplified as this.
    var names2 = from var {first, last} in persons
                select {first, last};


    io:println(names2);
}

Destructuring Records

Destructuring records is particularly useful with query expressions, but works anywhere you can have var. var is followed by a binding pattern. The semantics of binding pattern is open. {x} is short for {x: x} in both binding patterns and record constructors.

import ballerina/io;
type Person record {
 string first;
 string last;
 int yearOfBirth;
};
public function main() {
    Person[] persons = [
        {first: "Melina", last: "Kodel", yearOfBirth: 1994},
        {first: "Tom", last: "Riddle", yearOfBirth: 1926}
    ];
    var names1 = from var {first: f, last: l} in persons
                select {first: f, last: l};

A Person record is destructured here, as a projection with first and last fields. {first: f, last: l} is the binding pattern.

    io:println(names1);
    var names2 = from var {first, last} in persons
                select {first, last};

The same can be simplified as this.

    io:println(names2);
}
bal run destructuring_records.bal
[{"first":"Melina","last":"Kodel"},{"first":"Tom","last":"Riddle"}]
[{"first":"Melina","last":"Kodel"},{"first":"Tom","last":"Riddle"}]
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