From fe3f5b1fec1802ff5dea7faebafe69e07d22c80a Mon Sep 17 00:00:00 2001 From: Heshan Padmasiri Date: Mon, 7 Oct 2024 14:13:28 +0530 Subject: [PATCH] Update error intersection --- .../error_type_intersection.bal | 66 +++++++++++-------- .../error_type_intersection.md | 2 +- .../error_type_intersection.out | 4 +- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/examples/error-type-intersection/error_type_intersection.bal b/examples/error-type-intersection/error_type_intersection.bal index cd904bcb04..cd0c1d7992 100644 --- a/examples/error-type-intersection/error_type_intersection.bal +++ b/examples/error-type-intersection/error_type_intersection.bal @@ -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`. -// An error value belongs to this type if and only if it belongs to both `IOError` -// and `error`. -type FileIOError IOError & error; +type InputError error; + +type NumericError error; + +type DistinctInputError distinct error; + +type DistinctNumericError distinct error; + +// 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`. - io:println(fileIOError is IOError); - io:println(fileIOError is error); - - // An `IOError` value will not belong to `FileIOError` if it doesn't belong to - // `error`. - IOError ioError = error("invalid input"); - io:println(ioError is FileIOError); - - // Similarly, an error value belonging to `error` will not belong - // to `FileIOError` if it doesn't belong to `IOError`. - error 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); } diff --git a/examples/error-type-intersection/error_type_intersection.md b/examples/error-type-intersection/error_type_intersection.md index 2b65eb1bb1..8493c9da00 100644 --- a/examples/error-type-intersection/error_type_intersection.md +++ b/examples/error-type-intersection/error_type_intersection.md @@ -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 ::: diff --git a/examples/error-type-intersection/error_type_intersection.out b/examples/error-type-intersection/error_type_intersection.out index e315509605..20c659f272 100644 --- a/examples/error-type-intersection/error_type_intersection.out +++ b/examples/error-type-intersection/error_type_intersection.out @@ -1,5 +1,5 @@ $ bal run error_type_intersection.bal true -true -false false +true +true