Function to exit the program without a panic? #3559
-
Is there a way to exit a gleam program or task without getting an error? You can If such a function is added, an assosiated type should be added, being simply a type with no variants - making it impossible to construct, clearly showing that the function will not return (simple demo implementation below)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
What would be a use case for this? Maybe FFI could work instead. Although I've heard early exits aren't the best for the Erlang VM. // main.gleam
import gleam/io
pub fn main() {
io.println("Hello!")
exit(0)
io.println("this never prints")
}
@external(erlang, "exit_ffi", "do_exit")
@external(javascript, "./exit_ffi.mjs", "do_exit")
pub fn exit(exit_code: Int) -> Nil % exit_ffi.erl
-module(exit_ffi).
-export([do_exit/1]).
do_exit(Code) ->
halt(Code). // exit_ffi.mjs
export function do_exit(code) {
if (globalThis.process) process.exit(code);
else if (globalThis.Deno) Deno.exit(code);
else throw Error; // if in the browser
} |
Beta Was this translation helpful? Give feedback.
Then you want init.stop, not erlang.halt