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
|
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