Swan Lake Beta2 is here.
import ballerina/io;

type Employee record {|
    readonly int id;
    string firstName;
    string lastName;
    int salary;
|};

public function main() {
    table<Employee> key(id) employees = table [
        {id: 1, firstName: "John", lastName: "Smith", salary: 100},
        {id: 2, firstName: "Fred", lastName: "Bloggs", salary: 200}
    ];

    // `from` clause iterates `employees` `table`.
    // The contextually-expected type of the `query expression` is an `int[]`.
    int[] salaries = from var {salary} in employees
                     select salary;


    io:println(salaries);
}

Querying Tables

Tables can be nicely combined with query expressions unlike maps. Basic type of the output of query expression is determined by the contextually expected type and the input type.

import ballerina/io;
type Employee record {|
    readonly int id;
    string firstName;
    string lastName;
    int salary;
|};
public function main() {
    table<Employee> key(id) employees = table [
        {id: 1, firstName: "John", lastName: "Smith", salary: 100},
        {id: 2, firstName: "Fred", lastName: "Bloggs", salary: 200}
    ];
    int[] salaries = from var {salary} in employees
                     select salary;

from clause iterates employees table. The contextually-expected type of the query expression is an int[].

    io:println(salaries);
}
bal run querying_tables.bal
[100,200]
import ballerina/io;

type Employee record {|
    readonly int id;
    string firstName;
    string lastName;
    int salary;
|};

public function main() {
    table<Employee> key(id) employees = table [
        {id: 1, firstName: "John", lastName: "Smith", salary: 100},
        {id: 2, firstName: "Fred", lastName: "Bloggs", salary: 200}
    ];

    // `from` clause iterates `employees` `table`.
    // The contextually-expected type of the `query expression` is an `int[]`.
    int[] salaries = from var {salary} in employees
                     select salary;


    io:println(salaries);
}

Querying Tables

Tables can be nicely combined with query expressions unlike maps. Basic type of the output of query expression is determined by the contextually expected type and the input type.

import ballerina/io;
type Employee record {|
    readonly int id;
    string firstName;
    string lastName;
    int salary;
|};
public function main() {
    table<Employee> key(id) employees = table [
        {id: 1, firstName: "John", lastName: "Smith", salary: 100},
        {id: 2, firstName: "Fred", lastName: "Bloggs", salary: 200}
    ];
    int[] salaries = from var {salary} in employees
                     select salary;

from clause iterates employees table. The contextually-expected type of the query expression is an int[].

    io:println(salaries);
}
bal run querying_tables.bal
[100,200]

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