Swan Lake Beta3 is here.
import ballerina/io;

// `distinct` creates a new subtype.
type XErr distinct error;
type YErr distinct error;

type Err XErr|YErr;

// The name of the distinct type can be used with the error
// constructor to create an error value of that type.
// `err` holds an error value of type `XErr`.
Err err = error XErr("Whoops!");

function desc(Err err) returns string {
    // The `is` operator can be used to distinguish distinct subtypes.
    return err is XErr ? "X" : "Y";

}

public function main() {
    io:println(desc(err));
}

Error Subtyping

distinct creates a new subtype and can be used to define subtypes of error. The name of the distinct error type can be used with the error constructor to create an error value of that type. Works like a nominal type: is operator can be used to distinguish distinct subtypes. Each occurrence of distinct has a unique identifier that is used to tag instances of the type.

import ballerina/io;
type XErr distinct error;
type YErr distinct error;

distinct creates a new subtype.

type Err XErr|YErr;
Err err = error XErr("Whoops!");

The name of the distinct type can be used with the error constructor to create an error value of that type. err holds an error value of type XErr.

function desc(Err err) returns string {
    return err is XErr ? "X" : "Y";

The is operator can be used to distinguish distinct subtypes.

}
public function main() {
    io:println(desc(err));
}
bal run error_subtyping.bal
X
import ballerina/io;

// `distinct` creates a new subtype.
type XErr distinct error;
type YErr distinct error;

type Err XErr|YErr;

// The name of the distinct type can be used with the error
// constructor to create an error value of that type.
// `err` holds an error value of type `XErr`.
Err err = error XErr("Whoops!");

function desc(Err err) returns string {
    // The `is` operator can be used to distinguish distinct subtypes.
    return err is XErr ? "X" : "Y";

}

public function main() {
    io:println(desc(err));
}

Error Subtyping

distinct creates a new subtype and can be used to define subtypes of error. The name of the distinct error type can be used with the error constructor to create an error value of that type. Works like a nominal type: is operator can be used to distinguish distinct subtypes. Each occurrence of distinct has a unique identifier that is used to tag instances of the type.

import ballerina/io;
type XErr distinct error;
type YErr distinct error;

distinct creates a new subtype.

type Err XErr|YErr;
Err err = error XErr("Whoops!");

The name of the distinct type can be used with the error constructor to create an error value of that type. err holds an error value of type XErr.

function desc(Err err) returns string {
    return err is XErr ? "X" : "Y";

The is operator can be used to distinguish distinct subtypes.

}
public function main() {
    io:println(desc(err));
}
bal run error_subtyping.bal
X
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