Swan Lake Beta2 is here.
import ballerina/io;

const KEY = "xyzzy";

function matchTest(any v) returns string {
    // The value of the `v` variable is matched against the
    // given value match patterns.
    match v {
        17 => {
            return "number";
        }
        true => {
            return "boolean";
        }
        "str" => {
            return "string";
        }
        KEY => {
            return "constant";
        }
        0|1 => {
            return "or";
        }
        _ => {
            return "any";
        }
    }

}

public function main() {
    io:println(matchTest("str"));
    io:println(matchTest(17));
    io:println(matchTest(20.5));
}

Match Statement

match statement is similar to switch statement in C and JavaScript. It matches the value, not the type. == is used to test whether left hand side matches the value being matched. Left hand side can be a simple literal (nil, boolean, int, float, string) identifier referring to a constant. Left hand side of _ matches if the value is of type any. You can use | to match more than one value.

import ballerina/io;
const KEY = "xyzzy";
function matchTest(any v) returns string {
    match v {
        17 => {
            return "number";
        }
        true => {
            return "boolean";
        }
        "str" => {
            return "string";
        }
        KEY => {
            return "constant";
        }
        0|1 => {
            return "or";
        }
        _ => {
            return "any";
        }
    }

The value of the v variable is matched against the given value match patterns.

}
public function main() {
    io:println(matchTest("str"));
    io:println(matchTest(17));
    io:println(matchTest(20.5));
}
bal run match_statement.bal
string
number
any
import ballerina/io;

const KEY = "xyzzy";

function matchTest(any v) returns string {
    // The value of the `v` variable is matched against the
    // given value match patterns.
    match v {
        17 => {
            return "number";
        }
        true => {
            return "boolean";
        }
        "str" => {
            return "string";
        }
        KEY => {
            return "constant";
        }
        0|1 => {
            return "or";
        }
        _ => {
            return "any";
        }
    }

}

public function main() {
    io:println(matchTest("str"));
    io:println(matchTest(17));
    io:println(matchTest(20.5));
}

Match Statement

match statement is similar to switch statement in C and JavaScript. It matches the value, not the type. == is used to test whether left hand side matches the value being matched. Left hand side can be a simple literal (nil, boolean, int, float, string) identifier referring to a constant. Left hand side of _ matches if the value is of type any. You can use | to match more than one value.

import ballerina/io;
const KEY = "xyzzy";
function matchTest(any v) returns string {
    match v {
        17 => {
            return "number";
        }
        true => {
            return "boolean";
        }
        "str" => {
            return "string";
        }
        KEY => {
            return "constant";
        }
        0|1 => {
            return "or";
        }
        _ => {
            return "any";
        }
    }

The value of the v variable is matched against the given value match patterns.

}
public function main() {
    io:println(matchTest("str"));
    io:println(matchTest(17));
    io:println(matchTest(20.5));
}
bal run match_statement.bal
string
number
any

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