Swan Lake Beta2 is here.
import ballerina/io;

// This is shorthand for,
//
// `const RED = "RED";`
//
// `const GREEN = "GREEN";`
//
// `const BLUE = "BLUE";`
//
// `type Color RED|GREEN|BLUE;`
enum Color {
    RED, GREEN, BLUE
}

// An `enum` member can explicitly specify an associated expression.
enum Language {
    ENG = "English",
    TL = "Tamil",
    SI = "Sinhala"
}

public function main() {
    io:println(RED);
    io:println(ENG);
}

Enumerations

Enumerations are shorthand for unions of string constants. A const can be used as a singleton type. An enum is not a distinct type. You can specify the string constants explicitly.

import ballerina/io;
enum Color {
    RED, GREEN, BLUE
}

This is shorthand for,

const RED = "RED";

const GREEN = "GREEN";

const BLUE = "BLUE";

type Color RED|GREEN|BLUE;

enum Language {
    ENG = "English",
    TL = "Tamil",
    SI = "Sinhala"
}

An enum member can explicitly specify an associated expression.

public function main() {
    io:println(RED);
    io:println(ENG);
}
bal run enumerations.bal
RED
English
import ballerina/io;

// This is shorthand for,
//
// `const RED = "RED";`
//
// `const GREEN = "GREEN";`
//
// `const BLUE = "BLUE";`
//
// `type Color RED|GREEN|BLUE;`
enum Color {
    RED, GREEN, BLUE
}

// An `enum` member can explicitly specify an associated expression.
enum Language {
    ENG = "English",
    TL = "Tamil",
    SI = "Sinhala"
}

public function main() {
    io:println(RED);
    io:println(ENG);
}

Enumerations

Enumerations are shorthand for unions of string constants. A const can be used as a singleton type. An enum is not a distinct type. You can specify the string constants explicitly.

import ballerina/io;
enum Color {
    RED, GREEN, BLUE
}

This is shorthand for,

const RED = "RED";

const GREEN = "GREEN";

const BLUE = "BLUE";

type Color RED|GREEN|BLUE;

enum Language {
    ENG = "English",
    TL = "Tamil",
    SI = "Sinhala"
}

An enum member can explicitly specify an associated expression.

public function main() {
    io:println(RED);
    io:println(ENG);
}
bal run enumerations.bal
RED
English

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