Hi all,
I've been working on some changes to swift package init --type executable
to improve scalability and maintainability based on feedback from the SwiftPM community. If you're interested in the code you can check out the pull request.
Here's a breakdown of the proposed changes to the executable template:
Current
// ./Sources/MyPkg.swift
print("Hello, world!")
Proposed
// ./Sources/MyPkg/MyPkg.swift
@main
struct MyPkg {
static func main() {
print("Hello, world!")
}
}
- @main annotated struct: Replace top-level code with an
@main
annotated struct. This avoid issues with top-level variable initialization and provides a clear entry point. - Subfolder Structure: Move the source file into a subfolder within Sources. This anticipates the eventual need for multiple targets, and avoids having to restructure the project's layout to add another target.
This change also brings the executable
and tool
template types more in line with each other, as --type tool
already used an @main
annotated struct.
Templates are a balance between simplicity and scalability/maintainability, and the goal is to move towards a template that scales well without the need for refactoring. I'm interested in your feedback so please let me know what you think about these changes.
8 posts - 7 participants