Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Introduce match expressions #1314

Open
Shadow-Devil opened this issue Jul 29, 2024 · 0 comments
Open

Proposal: Introduce match expressions #1314

Shadow-Devil opened this issue Jul 29, 2024 · 0 comments
Labels
Area/Lang Relates to the Ballerina language specification Type/NewFeature

Comments

@Shadow-Devil
Copy link

Description:
It would be great to have also match expressions, similar to match statements, where each branch has to return a value (of the same type), which can then be used. It would also be great to introduce a syntax that one can omit the curly braces if only a value is returned.

Example from here.
Instead of

const KEY = "xyzzy";

function mtest(any v) returns string {

    match v {
        17 => {
            return "number";
        }
        true => {
            return " boolean";
        }
        "str" => {
            return "string";
        }
        KEY => {
            return "constant";
        }
        0|1 => {
            return "or";
        }
        _ => {
            return "any";
        }
    }
}

one could write:

const KEY = "xyzzy";

function mtest(any v) returns string {

    return match v {
        17 => "number";
        true => "boolean";
        "str" => "string";
        KEY => "constant";
        0|1 => "or";
        _ => "any";
    }
}

This idea is mainly coming from Java's switch expressions.

Suggested Labels:

Code sample that shows issue:

Related Issues:

@anupama-pathirage anupama-pathirage added Type/NewFeature Area/Lang Relates to the Ballerina language specification labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Lang Relates to the Ballerina language specification Type/NewFeature
Projects
None yet
Development

No branches or pull requests

2 participants