Why is main() public in executable packages?

Hi,
I know that in Java the main method must be static and public due to the way it works, and Swift also has to be static, which also makes sense since it has to be on the struct itself. What I don't understand is why the default template when creating a package with --type executable has the main struct and method declared as public by default. It compiles just fine without either being public :eyes:
Why is it like that?

I thought that it's for the tests to see it, but even that seems to work fine with just

@main
struct Main {
    
    var text = "a"
    
    static func main() {
        print("hello")
    }
}

where the "a" can successfully be tested

I think you're rightβ€”I'm not aware of a compelling reason why they need to be public in the SwiftPM template, and from a progressive disclosure perspective for learners it's probably best to omit details that aren't necessary, precisely to avoid the sort of doubts arising that you raise.

/cc @NeoNacho @tomerd who might know more about whether the template can be refined :)

2 Likes

Okay :slight_smile:
By the way, on the topic of templates, I think it would be cool to have the ability to customise them.

I create packages often to try out different ideas and I have to spend a lot of time fixing the package.swift file every time.
The comments explaining what everything does are definitely useful when learning, but I don't think they're necessary after a while.

Unless there's already an option to do this that I missed?

cc @bitjammer have been thinking about this topic lately, she may have some ideas