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

allow multiple kinds of return types from main #535

Closed
andrewrk opened this issue Oct 14, 2017 · 3 comments
Closed

allow multiple kinds of return types from main #535

andrewrk opened this issue Oct 14, 2017 · 3 comments

Comments

@andrewrk
Copy link
Member

@andrewrk andrewrk commented Oct 14, 2017

Currently, this is the main entry point function:

pub fn main() -> %void {

}

The bootstrap code runs it, checks the return value, and makes the exit/ExitProcess syscall.

If a zig user wants to do their own process exiting, however, they should be able to make the return type noreturn, and then the bootstrap code does not need to handle cleanup.

This shaves off a couple of bytes of bloat for some of our more hardcore users such as demoscene programmers.

@thejoshwolfe
Copy link
Contributor

@thejoshwolfe thejoshwolfe commented Oct 16, 2017

Another usecase for returning noreturn is if a program can't stop, won't stop. This is common for daemons such as groovebasin.

@skyfex
Copy link

@skyfex skyfex commented Nov 29, 2017

Microcontroller firmware is one more usecase.

@andrewrk
Copy link
Member Author

@andrewrk andrewrk commented Jan 12, 2018

more proposal:

allow multiple kinds of return types from main

  • noreturn - main never returns
  • void - main does not fail
  • %void - possibly print an error return trace
switch (@typeId(@returnType(root.main))) {
    builtin.TypeId.NoReturn => {
        root.main();
    },
    builtin.TypeId.Void => {
        root.main();
        std.os.exit(0);
    },
    builtin.TypeId.ErrorUnion => {
        if (builtin.mode == builtin.Mode.ReleaseFast) {
            root.main() catch std.os.exit(1);
            std.os.exit(0);
        } else {
            root.main() catch |err, trace| {
                std.debug.warn("error: {}\n", @errorName(err));
                std.debug.dumpStackTrace(trace);
                std.os.exit(1);
            };
            std.os.exit(0);
        }
    },
    else => @compileError("expected return type of main to be 'noreturn', 'void', or '%void'"),
}

@andrewrk andrewrk changed the title allow return type of main to be noreturn allow multiple kinds of return types from main Jan 12, 2018
@andrewrk andrewrk closed this in 7b57454 Jan 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants