Swan Lake Beta3 is here.
import ballerina/io;

type StructuredName record {
    string firstName;
    string lastName;
};

// A `Name` type value can be either a `StructuredName` or a `string`.
type Name StructuredName|string;

public function main() {
    // `name1` is a `StructuredName`.
    Name name1 = {
        firstName: "Rowan",
        lastName: "Atkinson"
    };
    // `name2` is a `string`.
    Name name2 = "Leslie Banks";

    io:println(nameToString(name1));
    io:println(nameToString(name2));
}

function nameToString(Name nm) returns string {
    // Checks whether `nm` belongs to `string` type.
    if nm is string {

        return nm;
    } else {
        return nm.firstName + " " + nm.lastName;
    }
}

Unions

T1|T2 is the union of the sets described by T1 and T2. T? is completely equivalent to T|(). Unions are untagged. The is operator tests whether a value belongs to a specific type. is operator in the condition causes declared type to be narrowed.

import ballerina/io;
type StructuredName record {
    string firstName;
    string lastName;
};
type Name StructuredName|string;

A Name type value can be either a StructuredName or a string.

public function main() {
    Name name1 = {
        firstName: "Rowan",
        lastName: "Atkinson"
    };

name1 is a StructuredName.

    Name name2 = "Leslie Banks";

name2 is a string.

    io:println(nameToString(name1));
    io:println(nameToString(name2));
}
function nameToString(Name nm) returns string {
    if nm is string {

Checks whether nm belongs to string type.

        return nm;
    } else {
        return nm.firstName + " " + nm.lastName;
    }
}
bal run unions.bal
Rowan Atkinson
Leslie Banks
import ballerina/io;

type StructuredName record {
    string firstName;
    string lastName;
};

// A `Name` type value can be either a `StructuredName` or a `string`.
type Name StructuredName|string;

public function main() {
    // `name1` is a `StructuredName`.
    Name name1 = {
        firstName: "Rowan",
        lastName: "Atkinson"
    };
    // `name2` is a `string`.
    Name name2 = "Leslie Banks";

    io:println(nameToString(name1));
    io:println(nameToString(name2));
}

function nameToString(Name nm) returns string {
    // Checks whether `nm` belongs to `string` type.
    if nm is string {

        return nm;
    } else {
        return nm.firstName + " " + nm.lastName;
    }
}

Unions

T1|T2 is the union of the sets described by T1 and T2. T? is completely equivalent to T|(). Unions are untagged. The is operator tests whether a value belongs to a specific type. is operator in the condition causes declared type to be narrowed.

import ballerina/io;
type StructuredName record {
    string firstName;
    string lastName;
};
type Name StructuredName|string;

A Name type value can be either a StructuredName or a string.

public function main() {
    Name name1 = {
        firstName: "Rowan",
        lastName: "Atkinson"
    };

name1 is a StructuredName.

    Name name2 = "Leslie Banks";

name2 is a string.

    io:println(nameToString(name1));
    io:println(nameToString(name2));
}
function nameToString(Name nm) returns string {
    if nm is string {

Checks whether nm belongs to string type.

        return nm;
    } else {
        return nm.firstName + " " + nm.lastName;
    }
}
bal run unions.bal
Rowan Atkinson
Leslie Banks
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