wire icon indicating copy to clipboard operation
wire copied to clipboard

wire: support function arguments as providers

Open vangent opened this issue 5 years ago • 4 comments

This doesn't work:

func injectBar(fn func() *Foo) *Bar {
	panic(wire.Build(fn, NewBar))
}

It currently panics. I have an easy fix for the panic, but it then results in:

var fn func() *example.com/foo.Foo is not a provider or a provider set

fn should be usable as a provider for Foo/*Foo.

vangent avatar Nov 16 '18 00:11 vangent

I don't know if I understand the rule that's being introduced here. Is your idea to support using any unnamed function type that is introduced into the dependency graph as a provider? Or just specifically passing arguments to wire.Build? I'm not sure I see the benefit, even with reviewing google/go-cloud#690.

zombiezen avatar Nov 16 '18 17:11 zombiezen

A provider is just a function no? From the README, "a function that can produce a value". It seems like wire.Build should therefore accept various forms of such functions to be used in the provider graph. Why is fn in the example above not usable? I'm not sure exactly what "unnamed" means -- its name in this context is fn. What's the rationale for not allowing it?

In terms of the benefit, I tried this out as a workaround for #7. wire.Bind does not allow methods to be used as providers, and this would be a way to get around that (by passing the method to the injector function and using it that way).

When I tried it it panicked, which is clearly not good. The first PR I sent fixes the panic and saves the error wire produces.

vangent avatar Nov 16 '18 18:11 vangent

I'm hoping to fix the panic for this sprint; adding real support for this is a separate discussion.

vangent avatar Nov 16 '18 20:11 vangent

google/go-cloud#724 fixed the panic and recorded the current error. Deferring till next sprint for more work.

vangent avatar Nov 16 '18 21:11 vangent