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

Can't pass arguments on "register" #45

Closed
gvalmon opened this issue Dec 21, 2015 · 10 comments
Closed

Can't pass arguments on "register" #45

gvalmon opened this issue Dec 21, 2015 · 10 comments
Labels

Comments

@gvalmon
Copy link

gvalmon commented Dec 21, 2015

For some reason I can't pass arguments to "register" method, getting error "Cannot invoke 'register' with an argument list of type '(BaseFetchDelegate.Type, name: String, (_, _) -> BaseFetchDelegate)'"

It works correctly if I remove the second argument (table).

container.register(BaseFetchDelegate.self, name: "tasksFetchDelegate") { (r,table) in
  return BaseFetchDelegate()
}
@yoichitgy
Copy link
Member

I think, in your case, Swift compiler cannot infer the type of table because the parameter is not used in the closure. If you specify the type explicitly or use table in the closure to infer its type, you can pass the argument to register.

container.register(BaseFetchDelegate.self, name: "tasksFetchDelegate") {
    (r: ResolverType, table: TableType) in // Specify the type explicitly like this

    // Or use `table` anywhere in the closure to infer its type.
    return BaseFetchDelegate()
}

@gvalmon
Copy link
Author

gvalmon commented Dec 21, 2015

it works, thanks )

@yoichitgy
Copy link
Member

Good to hear!

@brunopinheiro
Copy link

Hi,

I had the problem when trying to use arguments. After explicitly declare the type of the arguments, I was not able to successfully resolve a registered ServiceFactory.

It took me a while, but I noticed, after read the implementation, that the ServiceKey uses the arguments types to map the ServiceFactories. I was using a protocol as a type of the argument, and when trying to resolve the dependency, I was using the implementation. To solve the problem, I had to do:

var obj = MyProtocolImplementation()
container.resolve(MyClass.self, argument: obj as MyProtocol)!

For me, it wasn't an obvious detail. I think it'd be great to add a note on documentation about those things.

@yoichitgy
Copy link
Member

Hi @brunopinheiro, thank you for posting the issue and sorry for the bad experience😥

Currently this page has a document about the service key, but it might not be so obvious. Would you give me suggestion to improve the document? Maybe adding a trouble shooting section is better?

@brunopinheiro
Copy link

Hi @yoichitgy,

My bad... Sorry. I didn't read or pay enough attention to this section on documentation.

Well... in that case, as a suggestion, I think you could add a note explaining when do you need to explicitly define the type of the arguments, and add an example like this one:

class PetOwner {
   var pet: AnimalType
}

container.register(PetOwner.self) { (_: ResolverType, pet: AnimalType) in
   var petOwner = PetOwner()
   petOwner.pet = pet
   return petOwner
}

var cat = Cat(name: "Bilbo")

// Won't work, since we don't have any Registration Key matching [PetOwner, (Cat) -> PetOwner]
var petOwner = container.resolve(PetOwner.self, argument: cat)!

// This is the correct Registration Key [PetOwner, (AnimalType) -> PertOwner]
var petOwner = container.resolve(PetOwner.self, argument: cat as AnimalType)!

@yoichitgy
Copy link
Member

Thanks @brunopinheiro for the suggestion😀 I'll update the documentation shortly as #70.

@brunopinheiro
Copy link

Nice! Thanks Yoichi!

Em ter, 22 de mar de 2016 às 10:26, Yoichi Tagaya notifications@github.com
escreveu:

Thanks @brunopinheiro https://github.com/brunopinheiro for the
suggestion😀 I'll update the documentation shortly as #70
#70.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#45 (comment)

@yoichitgy
Copy link
Member

@brunopinheiro I updated the document. I modified your suggestion slightly but it was very helpful because I didn't have to think from scratch. 5b2d047

Thank you for your help😃

@brunopinheiro
Copy link

I'm glad I could help! :)
Thanks for your attention, Yoichi!

Em qua, 23 de mar de 2016 às 12:54, Yoichi Tagaya notifications@github.com
escreveu:

@brunopinheiro https://github.com/brunopinheiro I updated the document.
I modified your suggestion slightly but it was very helpful because I
didn't have to think from scratch. 5b2d047
5b2d047


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#45 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants