You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 throwexcept 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.
The text was updated successfully, but these errors were encountered:
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?
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 apanic
keyword which is in most respects synonymous with javascript'sthrow
except for the fact that panics cannot be caught and handled.Example:
Todos:
panic
keyword to the lexer. (ren/data/token
)Panic
variant to theExpr
ast type. (ren/ast/expr
)$panic
or similar. (ren/ir/core
)parse_panic
parser. (ren/query/parse
)panic
will be used as an expression but our imp IR hasThrow
as a statement. (ren/ir/imp
)Things to think about:
The text was updated successfully, but these errors were encountered: