-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Proposal: Make main
and panic
attributes, rather than reserved names
#5224
Comments
Some cons from discussion on Discord:
Added here for fairness. |
I'm generally in favour of getting rid of "magic reserved names" if possible, even if it doesn't seem high-priority to me personally. The two downsides I see in the proposed approach is that
Here's an alternative idea that communicates the "magic" more clearly: providing a comptime {
const std = @import("std");
std.start.registerMainFunction(myMainFunction); //pro: clearly identify that we're communicating configuration for std/start.zig
std.builtin.registerPanicHandler(myPanicHandler); //more fitting location I think, since @panic is a language-level construct, though I don't know where the handling code for this currently is.
std.io.setGlobalDefaultIoMode(.evented); //This strategy would also be applicable for other declarations we currently expect in the "root source file".
} This would clearly indicate that we're configuring
|
I do like that idea better. I hadn't even thought of |
Actually, we could just make |
Upon further reflection, this would be a large amount of work for little to no benefit, a lot of compiler complexity, and even some end user detriment -- there is now no longer a single place to look for all the entry points. I see no reason this should stay open. |
(REVISED -- see below)
Say someone didn't know about the implementation of the panic handler, and tried to implement a function by the name of
panic
. Their crashes would be very confusing, and they wouldn't know where to look to fix the problem.I propose, instead of reserving function names, we instead expose an attribute to mark a function as the entry point, the panic handler, or neither. Not only would this make clear the privileged nature of these functions, it would also allow us to implement more idiomatic names, like this (syntax inspired by #4285):
(
.access
is just a first idea -- I'm sure there's a better name.)There are three possibilities --
.Entry
for the entry point,.Panic
for the panic handler, and.Common
for everything else..Common
is the default..Entry
and.Panic
enforce parameter and return types. Exactly one.Entry
and at most one.Panic
is required per project -- if we liked, we could fall back tomain
andpanic
if these were not present, so this change doesn't have to be breaking.The text was updated successfully, but these errors were encountered: