Skip to content

The standard effect of throwing exceptions#332

Open
ppolesiuk wants to merge 3 commits intomasterfrom
stdlib-exception
Open

The standard effect of throwing exceptions#332
ppolesiuk wants to merge 3 commits intomasterfrom
stdlib-exception

Conversation

@ppolesiuk
Copy link
Copy Markdown
Member

This PR provides standard effect capability of throwing exceptions together with a simple first-class handler. It is not clear to me, if the exceptions should be named Exception or just Exn.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new standard-library exceptions capability and a first-class handler to support raising and handling exceptions as an effect in DBL/Fram.

Changes:

  • Introduces Exception effect capability with a polymorphic raise.
  • Adds hException first-class handler intended to invoke a provided function on raise.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +22 to +25
pub let hException f =
handler
Exception { effect raise x = f x }
end
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

This PR adds a new stdlib effect/handler, but there are no accompanying stdlib tests (compare to test/stdlib/State.fram). Consider adding test/stdlib/Exception.fram covering at least: a) normal return passes through, b) raise triggers the handler function and does not resume, and c) interaction with other handlers (nesting).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@copilot propose an implementation of such tests based on this feedback

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nice try. Proposed code parses (not exactly as intended), but fails to type-check. However, after some simple fixes everything works correctly. I'm not perfectly happy with the proposed tests, but it is a good starting point.

Copy link
Copy Markdown

Copilot AI commented Mar 24, 2026

@ppolesiuk I've opened a new pull request, #333, to work on those changes. Once the pull request is ready, I'll request review from you.

@ppolesiuk ppolesiuk marked this pull request as draft March 24, 2026 17:18
##}

{## The effect capability for exceptions. ##}
pub data Exception E X =
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure if Exception is a good name. It is quite long. Do you think Exn is better?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the shorter Exn. With the parameters, Exception _ _ is 13 characters.


It calls given function `f` when an exception is raised.
##}
pub let hException {X} f =
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What is the naming convention (or what it should be) for type parameters like X. Should the name be more informative, like Payload?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or Info? I'm not really that bothered by X, though.

@ppolesiuk ppolesiuk marked this pull request as ready for review March 24, 2026 18:46
@ppolesiuk ppolesiuk requested a review from Copilot March 24, 2026 18:46
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Collaborator

@wojpok wojpok left a comment

Choose a reason for hiding this comment

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

Implementation-wise everything is fine. This effect can be used to implement canonical error handling that returns Result E X or Option X, though it would be nice to have explicit implementation of those effects for the sake of boilerplate reduction. Nonetheless, it is a nice addition to stdlib

@forell forell changed the title The standard effect of throwing excpetions The standard effect of throwing exceptions Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants