Skip to content

Commit

Permalink
Add Warning methods onto Any to make working with them easier. (#6176)
Browse files Browse the repository at this point in the history
Adds `has_warnings`, `remove_warnings` and `throw_on_warning` extension methods.
  • Loading branch information
jdunkerley authored Apr 12, 2023
1 parent b72ab4a commit 4e92d76
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@
- [Added support for Date/Time columns in the Postgres backend and added
`year`/`month`/`day` operations to Table columns.][6153]
- [`Text.split` can now take a vector of delimiters.][6156]
- [Add `has_warnings`, `remove_warnings` and `throw_on_warning` extension
methods.][6176]
- [Implemented `Table.union` for the Database backend.][6204]
- [Array & Vector have the same methods & behavior][6218]

Expand Down Expand Up @@ -572,6 +574,7 @@
[6150]: https://github.com/enso-org/enso/pull/6150
[6153]: https://github.com/enso-org/enso/pull/6153
[6156]: https://github.com/enso-org/enso/pull/6156
[6176]: https://github.com/enso-org/enso/pull/6176
[6204]: https://github.com/enso-org/enso/pull/6204
[6077]: https://github.com/enso-org/enso/pull/6077
[6218]: https://github.com/enso-org/enso/pull/6218
Expand Down
30 changes: 29 additions & 1 deletion distribution/lib/Standard/Base/0.0.0-dev/src/Any.enso
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import project.Data.Pair.Pair
import project.Data.Range.Extensions
import project.Data.Text.Text
import project.Data.Vector.Vector
import project.Error.Error
import project.Errors.Common.Incomparable_Values
import project.Errors.Common.No_Such_Conversion
import project.Errors.Common.Type_Error
import project.Nothing.Nothing
import project.Meta
import project.Nothing.Nothing
import project.Panic.Panic
import project.Warning.Warning

from project.Data.Boolean import Boolean, True, False
from project.Data.Ordering import all
Expand Down Expand Up @@ -450,6 +452,32 @@ type Any
>> : (Any -> Any) -> (Any -> Any)
>> self ~that = x -> that (self x)

## Checks if any warnings (either all or of a specified type) are attached to the value.

Arguments:
- warning_type: The type to check if attached to the value. Defaults to any warning.
has_warnings : Any -> Boolean
has_warnings self warning_type=Any =
_ = warning_type
False

## Remove the warnings (either all or of a specified type) attached to the value.

Arguments:
- warning_type: The type to remove if attached to the value. Defaults to all warnings.
remove_warnings : Any -> Any
remove_warnings self warning_type=Any =
_ = warning_type
self

## Throws the first matching warning (either all or of a specified type) as a data flow error.

Arguments:
- warning_type: The type to throw if attached to the value. Defaults to all warnings.
throw_on_warning : Any -> Any
throw_on_warning self warning_type=Any =
_ = warning_type
self

## PRIVATE
Checks if the comparators for the given objects are both of the same type. If so,
Expand Down
28 changes: 21 additions & 7 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Warning.enso
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import project.Data.Numbers.Integer
import project.Data.Pair.Pair
import project.Data.Vector.Vector
import project.Error.Error
import project.Meta
import project.Nothing.Nothing
import project.Polyglot.Polyglot
import project.Runtime
Expand All @@ -28,7 +29,26 @@ type Warning
## ADVANCED
Are any warnings attached to the value?
has_warnings : Any -> Boolean
has_warnings value = has_warnings_builtin value
has_warnings value warning_type=Any =
Warning.get_all value . any (w-> w.value.is_a warning_type)

## Remove the warnings (either all or of a specified type) attached to the value.

Arguments:
- warning_type: The type to remove if attached to the value. Defaults to all warnings.
remove_warnings : Any -> Any
remove_warnings value warning_type=Any =
Warning.detach_selected_warnings value (w-> w.is_a warning_type) . first

## Throws the first matching warning (either all or of a specified type) as a data flow error.

Arguments:
- warning_type: The type to throw if attached to the value. Defaults to all warnings.
throw_on_warning : Any -> Any
throw_on_warning self warning_type=Any =
warnings = Warning.get_all self
first = warnings.find (w-> w.value.is_a warning_type) if_missing=Nothing
if first.is_nothing then self else Error.throw first.value

## ADVANCED
Gets all the warnings attached to the given value. Warnings are returned in the
Expand Down Expand Up @@ -251,12 +271,6 @@ create payload origin = @Builtin_Method "Warning.create"
attach_with_stacktrace : Any -> Any -> Vector Stack_Trace_Element -> Any
attach_with_stacktrace value warning origin = @Builtin_Method "Warning.attach_with_stacktrace"

## PRIVATE

Builtin function that sees if any warnings attached.
has_warnings_builtin : Any -> Array Warning
has_warnings_builtin value = @Builtin_Method "Warning.has_warnings_builtin"

## PRIVATE

Builtin function that gets all the warnings attached to the given value.
Expand Down
Loading

0 comments on commit 4e92d76

Please sign in to comment.