-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4388f8
commit fe3f5b1
Showing
3 changed files
with
43 additions
and
29 deletions.
There are no files selected for viewing
66 changes: 40 additions & 26 deletions
66
examples/error-type-intersection/error_type_intersection.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |