-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add API to provide varargs #120
Comments
This adds support for functions with a variadic number of arguments to be Provided and Invoked. We will declare a dependency on a slice of the variadic type when a variadic function is encountered. With #120, this will be changed to an optional depedency so that variadic constructors Just Work.
This needs further discussion. Currently, constructors that have variadic arguments cannot be called from So before we make variadic arguments optional, we have to figure out how, if After discussions with @alsamylkin and @akshayjshah, there are two solutions
CC @alsamylkin @akshayjshah @breerly @glibsm |
I'm fine with the first option above - treating all variadic arguments as unmet optional dependencies - in the short term. If we can't come to agreement quickly, let's land that change and continue discussion. For a 1.0 release, I think that the second option (providing variadic arguments as slices) is better. One of dig's design goals was to let plain-Go constructor functions work correctly without extra ceremony. Functional options are very common, so we shouldn't treat them as an advanced use case - they're regular constructors, and they should just work. Concretely, "no extra ceremony" means that for any constructor While I see @alsamylkin's point about the syntax for passing arguments, I don't think we should choose an implementation that allows only one option. Since varargs and slices are so closely related in Go, providing them as slices seems both simple and obvious to me. I suspect that most experienced Gophers will feel the same. |
I think we see the problem from different sides: callee and the caller. While the callee does receive a slice, the caller doesn't have it, the compiler is going to create it. DIG is the caller in this case, so it needs to find all the arguments with this type, but it doesn't support multiple instances. So DIG already fails the "no extra ceremony" approach with constructors that expect several arguments of the same type. This is how I see varargs and think it represents a larger portion of issues, that we think is ok to wrap with a struct and named fields. The reason I prefer the first option is that I can totally be wrong and we can add this feature without breaking changes, while if a second approach will become undesirable, it would be a breaking change. That's why I prefer a more conservative approach. Some stats about searching for keywords on Github: |
Dig constructors may now be variadic functions. These functions will be called with their dependencies as usual and no arguments will be passed for the variadic arguments. So a constructor `New(*Foo, *Bar, ...Option) *Baz` will be treated as `New(*Foo, *Bar) *Baz`. See #120 for more discussion.
Dig constructors may now be variadic functions. These functions will be called with their dependencies as usual and no arguments will be passed for the variadic arguments. So a constructor `New(*Foo, *Bar, ...Option) *Baz` will be treated as `New(*Foo, *Bar) *Baz`. See #120 for more discussion.
Retitling this and tagging it as a post-1.0 feature. |
Let's plan to revisit this now that we're post 1.0. Value groups are probably the way to go here. |
Add support to fx.Annotate for variadic functions. The last parameter of variadic functions is now treated as a slice. With this users can feed value groups into variadic functions. Ref uber-go/dig#120 Co-authored-by: Abhinav Gupta <abg@uber.com> Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>
* Clean up wording and variable naming * More language fixes
Add support to fx.Annotate for variadic functions. The last parameter of variadic functions is now treated as a slice. With this users can feed value groups into variadic functions. Ref uber-go/dig#120 Co-authored-by: Abhinav Gupta <abg@uber.com> Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>
Typically, variadic arguments are optional - the function needs to handle
len(varargs)==0
already. Making dig automatically treat varargs as optional would make a wider variety of plain-Go functions work out of the box.The text was updated successfully, but these errors were encountered: