Swan Lake Beta3 is here.
import ballerina/io;

function foo(json j) returns error? {
    match j {
        // Match statement can be used to match maps.
        // Patterns on the left hand side in a match statement can have variable
        // parts that can be captured.
        // Match semantics are open (may have fields other than those 
        // specified in the pattern).
        {command: "add", amount: var x} => {
            decimal n = check x.ensureType(decimal);
            add(n);
        }

        _ => {
            return error("invalid command");
        }
    }
}

decimal total = 0;

function add(decimal amount) {
    total += amount;
    io:println("Total: ", total);
}

public function main() returns error? {
    check foo({command: "add", amount: 100, status: "pending"}); 
    check foo({command: "add", amount: 10});   
    check foo({command: "subtract", amount: 100});    
}

Match Statement With Maps

Match statement can be used to match maps. Patterns on the left hand side in a match statement can have variable parts that can be captured. Useful for working directly with json. Match semantics are open (may have fields other than those specified in the pattern).

import ballerina/io;
function foo(json j) returns error? {
    match j {
        {command: "add", amount: var x} => {
            decimal n = check x.ensureType(decimal);
            add(n);
        }

Match statement can be used to match maps. Patterns on the left hand side in a match statement can have variable parts that can be captured. Match semantics are open (may have fields other than those specified in the pattern).

        _ => {
            return error("invalid command");
        }
    }
}
decimal total = 0;
function add(decimal amount) {
    total += amount;
    io:println("Total: ", total);
}
public function main() returns error? {
    check foo({command: "add", amount: 100, status: "pending"}); 
    check foo({command: "add", amount: 10});   
    check foo({command: "subtract", amount: 100});    
}
bal run match_statement_with_maps.bal
Total: 100.0
Total: 110.0
error: invalid command {}
import ballerina/io;

function foo(json j) returns error? {
    match j {
        // Match statement can be used to match maps.
        // Patterns on the left hand side in a match statement can have variable
        // parts that can be captured.
        // Match semantics are open (may have fields other than those 
        // specified in the pattern).
        {command: "add", amount: var x} => {
            decimal n = check x.ensureType(decimal);
            add(n);
        }

        _ => {
            return error("invalid command");
        }
    }
}

decimal total = 0;

function add(decimal amount) {
    total += amount;
    io:println("Total: ", total);
}

public function main() returns error? {
    check foo({command: "add", amount: 100, status: "pending"}); 
    check foo({command: "add", amount: 10});   
    check foo({command: "subtract", amount: 100});    
}

Match Statement With Maps

Match statement can be used to match maps. Patterns on the left hand side in a match statement can have variable parts that can be captured. Useful for working directly with json. Match semantics are open (may have fields other than those specified in the pattern).

import ballerina/io;
function foo(json j) returns error? {
    match j {
        {command: "add", amount: var x} => {
            decimal n = check x.ensureType(decimal);
            add(n);
        }

Match statement can be used to match maps. Patterns on the left hand side in a match statement can have variable parts that can be captured. Match semantics are open (may have fields other than those specified in the pattern).

        _ => {
            return error("invalid command");
        }
    }
}
decimal total = 0;
function add(decimal amount) {
    total += amount;
    io:println("Total: ", total);
}
public function main() returns error? {
    check foo({command: "add", amount: 100, status: "pending"}); 
    check foo({command: "add", amount: 10});   
    check foo({command: "subtract", amount: 100});    
}
bal run match_statement_with_maps.bal
Total: 100.0
Total: 110.0
error: invalid command {}
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