@main: Type-Based Program Execution

It's funny, but I misunderstood how this proposal would work at first. Turns out it just took a slightly different route than what I expected. This is what I expected to see:

@main 
struct NSApplicationMain<AppDelegate: NSApplicationDelegate > {
    static func main() {
        // NSApplication startup code using the chosen delegate type
    }
}

And then you could apply this type as an attribute to your application delegate type:

@NSApplicationMain
class MyAppDelegate: NSApplicationDelegate {
   ...
}

And the generated main.swift would look like this:

NSApplicationMain<MyAppDelegate>.main()

So what I had in mind was a way to define custom attributes that could act exactly the same way as the built-in @UIApplicaitonMain and @NSApplicationMain we have today. We could even move the built-in ones out of the compiler and into their respective frameworks.


Using this proposal, it'd have to work something like this:

extension NSApplicationDelegate {
    static func main() { /* NSApplication startup code */ }
}
@main
class MyAppDelegate: NSApplicationDelegate {
   ...
}

I find that to be a strange pattern. It sorts of establishes a call hierarchy of this kind: Delegate.mainNSApplicationdelegate methods, where the application object is sandwiched in the delegate.

1 Like