Skip to content

Commit

Permalink
Update error intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Oct 8, 2024
1 parent e4388f8 commit fe3f5b1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
66 changes: 40 additions & 26 deletions examples/error-type-intersection/error_type_intersection.bal
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
import ballerina/io;

type IOError distinct error;
type InputErrorDetail record {|
int|string value;
|};

type FileErrorDetail record {
string filename;
};
type NumericErrorDetail record {|
int|float value;
|};

// The `FileIOError` type is defined as an intersection type using the `&` notation.
// It is the intersection of two error types: `IOError` and `error<FileErrorDetail>`.
// An error value belongs to this type if and only if it belongs to both `IOError`
// and `error<FileErrorDetail>`.
type FileIOError IOError & error<FileErrorDetail>;
type InputError error<InputErrorDetail>;

type NumericError error<NumericErrorDetail>;

type DistinctInputError distinct error<InputErrorDetail>;

type DistinctNumericError distinct error<NumericErrorDetail>;

// NumericInputError has detail type, record {| int value |}
type NumericInputError InputError & NumericError;

// DistinctNumericInputError has type ids of both DistinctInputError and DistinctNumericError
type DistinctNumericInputError DistinctInputError & DistinctNumericError;

function createNumericInputError(int value) returns NumericInputError {
return error("Numeric input error", value = value);
}

function createDistinctNumericInputError(int value) returns DistinctNumericInputError {
return error("Distinct numeric input error", value = value);
}

public function main() {
// In order to create an error value that belongs to `FileIOError`, the `filename`
// detail field must be provided.
FileIOError fileIOError = error("file not found", filename = "test.txt");

// `fileIOError` belongs to both `IOError` and `error<FileErrorDetail>`.
io:println(fileIOError is IOError);
io:println(fileIOError is error<FileErrorDetail>);

// An `IOError` value will not belong to `FileIOError` if it doesn't belong to
// `error<FileErrorDetail>`.
IOError ioError = error("invalid input");
io:println(ioError is FileIOError);

// Similarly, an error value belonging to `error<FileErrorDetail>` will not belong
// to `FileIOError` if it doesn't belong to `IOError`.
error<FileErrorDetail> fileError = error("cannot remove file", filename = "test.txt");
io:println(fileError is FileIOError);
NumericInputError e1 = createNumericInputError(5);
// e1 belong to InputError since it's detail type is a subtype of InputErrorDetail
io:println(e1 is InputError);

// e1 doesn't belong to DistinctInputError since it doesn't have the type id of DistinctInputError
io:println(e1 is DistinctInputError);

DistinctNumericInputError e2 = createDistinctNumericInputError(5);
// e2 belong to InputError since it's detail type is a subtype of InputErrorDetail
io:println(e2 is InputError);

// e2 belong to DistinctInputError since it's type id set include the type id of DistinctInputError
io:println(e2 is DistinctInputError);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type intersection for error types

You can define an error type that is both a subtype of a `distinct` error type and has additional constraints on the detail fields using intersection types.
If you intersect two `error` types, the resulting type's detail type is the intersection of the detail types of both types. Furthermore, if any of the types being intersected is a distinct type, then the resultant type's type ID set includes all the type IDs of that type.

::: code error_type_intersection.bal :::

Expand Down
4 changes: 2 additions & 2 deletions examples/error-type-intersection/error_type_intersection.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$ bal run error_type_intersection.bal
true
true
false
false
true
true

0 comments on commit fe3f5b1

Please sign in to comment.