Skip to content
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

Add variadic-generic Service.init overload #78

Open
1 task done
bbrk24 opened this issue Sep 25, 2023 · 3 comments
Open
1 task done

Add variadic-generic Service.init overload #78

bbrk24 opened this issue Sep 25, 2023 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@bbrk24
Copy link
Contributor

bbrk24 commented Sep 25, 2023

Checklist

Problem

When a single type has a lot of dependencies, the registration gets long and repetitive, and is fragile to changing the init:

Service(.transient, FooProtocol.self) { r in
  Foo(
    bar: r.resolve(),
    baz: r.resolve(),
    qux: r.resolve(),
    // ...
  )
}

Feature Request

As of Swift 5.9 (Xcode 15), we can add an overload like this:

#if swift(>=5.9)
extension Service {
    @available(swift 5.9)
    public init<TRegistration, each TDependency>(
        _ scope: Scope,
        _ type: TRegistration.Type,
        name: String? = nil,
        _ callback: @escaping (repeat each TDependency) -> TRegistration
    ) {
        self.init(scope, type, name: name) { (r: Resolver) in
            callback(repeat r.resolve((each TDependency).self))
        }
    }
}
#endif

each TDependency represents a "parameter pack"; the number of generic arguments to this initializer is variable. With this overload, one can say:

Service(.transient, FooProtocol.self, Foo.init)

I tested this locally and it works without confusing the compiler on existing registrations. However, I tested this on Linux, so I had to disable everything related to SwiftUI/Combine. I don't have a Mac to test those parts myself.

Value

The Factory.register blocks would become shorter, making them easier to read and write. Most callbacks passed to the Service are just an initializer.

Something similar could be done for MultitypeService.

@bbrk24 bbrk24 added the enhancement New feature or request label Sep 25, 2023
@bbrk24
Copy link
Contributor Author

bbrk24 commented Sep 26, 2023

@wboyd600 Have you updated to Xcode 15 yet? I could create the PR, but as I said I can't fully test it myself.

@wboyd600
Copy link
Contributor

Hey @bbrk24, hope you're doing well.

I just read up on all of the new features in 5.9. As far as the feature request goes, this definitely seems like an ideal use case for variadic generics and gets rid of a lot of boiler plate code.

I haven't updated to Xcode 15 yet! I'm always a little hesitant to update to X.0 releases, but I'm sure I'll end up updating soon enough (whether that's on purpose or not...).

I'll let you know when I do so I can fully test everything related to SwiftUI/Combine.

@wboyd600
Copy link
Contributor

wboyd600 commented Feb 1, 2024

Sorry that took forever -- but we did finally get around to updating!

@bbrk24 bbrk24 added this to the Backlog milestone Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants