-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Use @main
instead of top level code when initializing executable
#8371
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was changed to use top-level code instead of @main
not so long ago in #6144, what's the reason for reverting?
26a1fcc
to
488e018
Compare
Yeah, this is a pretty major change and not very new user friendly. Maybe this is a separate template? |
Top level code has various bugs that make it very easy to write code that miscompiles e.g.: print(x)
var x: Set = ["Hello World"] which crashes at runtime because x isn't initialized before we attempt to print it. This isn't a great user experience. I think until this is sorted we should prefer an |
488e018
to
eb0176b
Compare
I generally support this change. There should ideally be only one way to define a main entry point, and it should be an explicit form. If top-level code is truly problematic, then we should remove it from the language and be done with it. It would be nice if we could put I would ask that you consider naming the file/structure something that reflects the target, package, or a noun that reflects what it is, rather than just |
Another aspect of this template, if you are going to @main the main, then you should probably also create the module in a subdirectory of Sources. I believe that's why the current template is like it is, this is a different use case. The original template is actually an executable package. There is no easy mechanism to add a second module, as you well know. Which is a back to my point that this, where you're adding a module that's an executable is a different template. |
Since `@main` is the general purpose way of marking the entrypoint to executables, use it over top level code when generating an executable template.
b1d97a9
to
405b5ac
Compare
+1 for this change! Having the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM otherwise
Co-authored-by: David Nadoba <[email protected]>
To avoid users having to refactor this later as soon as they want to introduce a second module, place templated code in `/Sources/PkgName`. Partially reverts swiftlang#6144
At @dschaefer2's suggestion I've updated the PR to place the generated Source files for executables/tools into |
Love this change! I think we should give the community a heads up in the forums though. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor typo
Co-authored-by: Joseph Heck <[email protected]>
@heckj Thanks! |
@swift-ci test |
Do you mean |
@gavineadie Sorry, I was unclear, |
Motivation:
Since
@main
is the general purpose way of marking the entry point to executables, use it over top level code when generating an executable template.Modifications:
Update the template output when the specified
--type
is executable.Result:
The template is now:
This patch reverts #6197 and partially reverts #6144