Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Examples / Unit Tests for Exceptions #775

Merged
merged 18 commits into from
Jan 15, 2025

Conversation

JakubSchwenkbeck
Copy link
Contributor

@JakubSchwenkbeck JakubSchwenkbeck commented Jan 13, 2025

Motivation

Resolves #480
This module demonstrates exception handling combinators in Effekt using a simple, focused example. By showcasing custom exception handling (TestException) with different strategies (default handling, ignoring, reporting, finalizing), it provides a practical guide for understanding these features.

Changes

  • Introduced TestException as a custom exception type, independent of other exceptions like OutOfBounds.
  • Added generalOperation, which raises a TestException for invalid input or returns a string.
  • Included tests for handling exceptions with:
    • Default behavior
    • Ignoring exceptions
    • Reporting exceptions
    • Finalization hooks
    • "Reifying" with Results

Testing

The examples are self-contained in the main function, covering all provided exception-handling strategies. Each approach is tested and outputs expected results for validation (combinators.check).

@JakubSchwenkbeck
Copy link
Contributor Author

JakubSchwenkbeck commented Jan 14, 2025

Test Error: for llvm and chez-callcc the functions MissingValue() and raise("Some Text") seem to not be supported

Currently working on better and more natural examples which occur in the programflow without the need of these two functions

@jiribenes
Copy link
Contributor

I'm not sure about the value of testing all the different exceptions as in types: Exception[E] is generic in E, so once you test one, you've tested them all by parametricity ;).
I'd guess the more important thing is testing all of the on[E].xyz combinators here:

def ignore[E](proxy: on[E]) { prog: => Unit / Exception[E] }: Unit =
ignore[E] {prog}
def default[E, T](proxy: on[E]) { d: => T } { prog: => T / Exception[E] }: T =
default[E, T] {d} {prog}
def panic[E, T](proxy: on[E]) { prog: => T / Exception[E] }: T =
panicOn[E, T] {prog}
def report[E](proxy: on[E]) { prog: => Unit / Exception[E] }: Unit =
report[E] {prog}
def finalize[E, T](proxy: on[E]) { finalizer: => Unit } { prog: => T / Exception[E] }: T / Exception[E] =
finalize[E, T] {finalizer} {prog}
/ their full versions & perhaps the one more in result.effekt too :)

@JakubSchwenkbeck
Copy link
Contributor Author

Thank you for your input and help, I really appreciate it!
Feel free to tell me if the current approach is of more sense:)

Copy link
Contributor

@jiribenes jiribenes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few style-related nitpicks, but otherwise LGTM, thanks! :shipit:

examples/stdlib/exception/combinators.effekt Outdated Show resolved Hide resolved
examples/stdlib/exception/combinators.effekt Outdated Show resolved Hide resolved
examples/stdlib/exception/combinators.effekt Outdated Show resolved Hide resolved
@jiribenes
Copy link
Contributor

Resolves #480.

@jiribenes jiribenes linked an issue Jan 15, 2025 that may be closed by this pull request
@@ -60,9 +58,9 @@ def main() = {
finalizeTestException { str.generalOperation(0) } // Test: Finalizer
finalizeTestException { str.generalOperation(1) } // Test: Finalizer hello

// Test for "reifying" an Exception using Result
// Test for "reifying" an Exception usix^xng Result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Test for "reifying" an Exception usix^xng Result
// Test for "reifying" an Exception using Result

typo :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking over it!:)

Copy link
Contributor

@jiribenes jiribenes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, let's ship it! :shipit:

@jiribenes jiribenes merged commit eb7febb into effekt-lang:master Jan 15, 2025
2 checks passed
EveEme pushed a commit to EveEme/effekt that referenced this pull request Jan 20, 2025
## Motivation
Resolves effekt-lang#480
This module demonstrates exception handling combinators in Effekt using
a simple, focused example. By showcasing custom exception handling
(`TestException`) with different strategies (default handling, ignoring,
reporting, finalizing), it provides a practical guide for understanding
these features.

## Changes
- Introduced `TestException` as a custom exception type, independent of
other exceptions like `OutOfBounds`.
- Added `generalOperation`, which raises a `TestException` for invalid
input or returns a string.
- Included tests for handling exceptions with:
  - Default behavior
  - Ignoring exceptions
  - Reporting exceptions
  - Finalization hooks
  - "Reifying" with Results

## Testing
The examples are self-contained in the `main` function, covering all
provided exception-handling strategies. Each approach is tested and
outputs expected results for validation (`combinators.check`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tests for exception stdlib.
2 participants