Skip to content

juicy main #24510

@andrewrk

Description

@andrewrk

What if you could receive a bunch of goodies in main simply by asking for them?

pub fn main(init: std.process.Init, args: Args) anyerror!void { ... }

The first arg:

pub const Init = struct {
    /// Permanent storage for the entire process, cleaned automatically on exit.
    arena: *std.heap.ArenaAllocator,
    /// A default-selected general purpose allocator for temporary heap allocations.
    /// Debug mode will set up leak checking.
    gpa: std.mem.Allocator,
    /// An appropriate default Io implementation based on the target configuration.
    /// Debug mode will set up leak checking.
    io: std.Io,
    /// Environment variables.
    environ: std.process.Environ,
    /// Os-specific stuff.
    os: union {
        linux: Aux,
        windows: Peb,
        // ...
    },
};

The second arg is user-defined is does that CLI pattern where you provide a type that contains already parsed CLI args, and then the start code uses type reflection to parse the args before calling main:

const Args = struct {
    named: struct {
        example: u32,
    },
    /// Memory allocated into Init.arena
    positional: []const []const u8,
};

This would be a minimal, opinionated parser that users would opt out of the moment they want to do something remotely sophisticated, by accepting []const []const u8 instead of a struct.

If you think about it we already have a little bit of this with the return type - if it returns an error, start code does some opinionated stuff when an error is returned, such as print an error return trace (debug) or the error name (release) and exit(1).

Users who want full control over everything can continue to use pub fn main() noreturn {} and have the start code do almost nothing (beyond setting up Thread Local Storage, signal handling, PIE, etc., as it already does).

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.enhancementSolving this issue will likely involve adding new logic or components to the codebase.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions