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

Safer exceptions #11721

Draft
wants to merge 17 commits into
base: master
from
Draft

Safer exceptions #11721

wants to merge 17 commits into from

Conversation

@odersky
Copy link
Contributor

@odersky odersky commented Mar 12, 2021

Introduce a flexible scheme for declaring and checking which exceptions can be thrown.
It relies on the effects as implicit capabilities pattern.

The scheme is not 100% safe yet since it does not track and prevent capability capture.
Nevertheless, it's already useful for declaring thrown exceptions and finding mismatches
between provided and required capabilities.

Link to Doc page

Everything is enabled under

import language.experimental.saferExceptions

Based on #11695

odersky added 13 commits Mar 10, 2021
Introduce a flexible scheme for declaring and checking exceptions that can be thrown.
It relies on the _effects as implicit capabilities_ pattern.

The scheme is not 100% safe yet since it does not track and prevent capability capture.
Nevertheless, it's already useful for declaring thrown exceptions and finding mismatches
between provided and required capabilities.
@odersky odersky changed the title Introduce checked exceptions Safer exceptions Mar 12, 2021
@odersky odersky force-pushed the dotty-staging:add-safe-throws-2 branch from 4c2ceb6 to 0cbaa03 Mar 14, 2021
@odersky odersky force-pushed the dotty-staging:add-safe-throws-2 branch from 0cbaa03 to b65958f Mar 14, 2021
@odersky odersky force-pushed the dotty-staging:add-safe-throws-2 branch from 0228129 to fe446b4 Mar 14, 2021
@soronpo
Copy link
Collaborator

@soronpo soronpo commented Mar 14, 2021

Regarding multiple exceptions, IIUC, the current concept is that the return signature can be something like T canThrow Ex1 canThrow Ex2. Isn't that signature considered different than T canThrow Ex2 canThrow Ex1 ?

Actually, no. The rules for context function types make them mutually compatible. I am not yet sure about unions, whether there is a way to obtain them. But since we do have commutativity, maybe they are not needed.

@odersky odersky force-pushed the dotty-staging:add-safe-throws-2 branch 3 times, most recently from 648c880 to c02fb16 Mar 14, 2021
@odersky odersky force-pushed the dotty-staging:add-safe-throws-2 branch from c02fb16 to 1c353ab Mar 14, 2021
@@ -680,6 +680,8 @@ object Build {
"-sourcepath", (sourceDirectories in Compile).value.map(_.getAbsolutePath).distinct.mkString(File.pathSeparator),
// support declaration of scala.compiletime.erasedValue
"-Yerased-terms"
// TODO: drop after bootstrap with erasure language import
// scala.compile now contains the roght language import so no global setting is needed

This comment has been minimized.

@mr-git

mr-git Mar 15, 2021

maybe:

.. the right language ..
?

import language.experimental.erasedTerms
import annotation.implicitNotFound

/** A ability class that allows to throw exception `E`. When used with the

This comment has been minimized.

@mr-git

mr-git Mar 15, 2021

/** AN ability ..

though, in other places it is referenced as "A capability"..

@Jasper-M
Copy link
Contributor

@Jasper-M Jasper-M commented Mar 16, 2021

Would it be possible for a user to implement something like this, without needing casts or other hacks?

object Try:
  def apply[E, A](body: CanThrow[E] ?=> A): Try[A] = 
    try
      Success(body)
    catch
      case NonFatal(ex) => Failure(ex)
@YulawOne
Copy link

@YulawOne YulawOne commented Mar 17, 2021

I would like to suggest, to consider rename CanThrow and canThrow to Throws and throws, a reasoning behind is it looks quite awkward with camelCase and would be much neater with single word. Alternative names can be - raises,raise, cause, fails or failure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants