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

Resolving dependencies with protocol arguments #180

Closed
irace opened this issue Oct 20, 2016 · 1 comment
Closed

Resolving dependencies with protocol arguments #180

irace opened this issue Oct 20, 2016 · 1 comment

Comments

@irace
Copy link

irace commented Oct 20, 2016

Hello,

It is possible to do something like this, where the argument passed to the dependency container when trying to resolve a dependency is typed as a protocol?

protocol ArgumentType {}
struct Dependency {}

let container = Container()
container.register(Dependency.self) { (container: ResolverType, argument: ArgumentType) in
  return Dependency()
}

extension String: ArgumentType {}

let dependency = container.resolve(Dependency.self, argument: "I’m the argument!")

This fails to find the registered dependency, this way. If I change the registration to specify String as the argument type, instead of ArgumentType:

container.register(Dependency.self) { (container: ResolverType, argument: String) in
  return Dependency()
}

Then it resolves as expected.

Please advise – thank you!

@irace
Copy link
Author

irace commented Oct 20, 2016

OK, figured it out. This works so long as you do:

let dependency = container.resolve(Dependency.self, argument: "I’m the argument!" as ArgumentType)

@irace irace closed this as completed Oct 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant