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 panic keyword to terminate a program. #71

Open
5 tasks
hayleigh-dot-dev opened this issue Sep 13, 2021 · 2 comments
Open
5 tasks

✨ Add panic keyword to terminate a program. #71

hayleigh-dot-dev opened this issue Sep 13, 2021 · 2 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@hayleigh-dot-dev
Copy link
Contributor

hayleigh-dot-dev commented Sep 13, 2021

Sometimes you just want to bail out of a program entirely because something has gone so wrong it is unrecoverable. This is often achieved by throwing or raising an Exception, with the understanding that the caller may catch that exception and recover in some other way.

With enums we already have a way to signal errors (by returning #error "..." for example) but we do not have a way to terminate the program prematurely. For that I think we should introduce a panic keyword which is in most respects synonymous with javascript's throw except for the fact that panics cannot be caught and handled.

Example:

pub let to_number = fun x ->
  switch x on
    case @Num n -> #just n
    case @Str s -> Str s
    case _         -> panic "Cannot coerce value to number."

Todos:

  • Add a panic keyword to the lexer. (ren/data/token)
  • Add a Panic variant to the Expr ast type. (ren/ast/expr)
    • Think about how to represent this in the core fn IR. We can probably represent this in the core IR as a built-in function $panic or similar. (ren/ir/core)
  • Add a parse_panic parser. (ren/query/parse)
  • Transform to sensible imp IR, remembering that in Ren panic will be used as an expression but our imp IR has Throw as a statement. (ren/ir/imp)

Things to think about:

  • In the example above, we're using a string for the error. @Alpvax below wonders whether other expressions should be valid. For now it may be easiest to do what Gleam currently does and not allow anything until we have a clearer design in the future.
  • Either way we should think about what the thrown error should actually be, what information do we want to surface to users and how will we make that information available.
@Alpvax
Copy link
Contributor

Alpvax commented Sep 15, 2021

What will the example compile to? What is a valid expression to follow the panic keyword?
If it compiles to throw what stops people catching and handling it?

I like the idea though

@hayleigh-dot-dev
Copy link
Contributor Author

  • it's a throw, yeah
  • strings, but it can probably be anything
  • there's no catch so the only way you'd do that is with FFI fuckery and FFI is "here be dragons"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants