Open kklimonda-fn opened 8 months ago
Hello! This is working as expected.
For background, the concept of typed nils comes from Go.
var v *http.Client = nil
var i HTTPClient = v
fmt.Println(v == nil) // true
fmt.Println(i == nil) // false
// https://go.dev/play/p/ZZS2T9XhMnb
It's a common gotcha, but it's completely valid to have a nil implementation in a non-nil interface.
type myClient struct{}
func (c *myClient) Do(req *http.Request) (*http.Response, error) {
if c == nil {
return http.DefaultClient.Do(req)
}
panic("TODO: custom logic here")
}
// Same as before:
var c *myClient = nil
var i HTTPClient = c
fmt.Println(i == nil) // false
// Can still send requests:
res, err := i.Do(req)
// ...
By providing a function that returns a nil *http.Client
, and casting it to an HTTPClient
, we're asking for a non-nil HTTPClient
with a nil *http.Client
inside it.
From Fx's point of view, this is behaving as expected. Fx should not be trying to glean the meaning of a typed nil. It should not discard my nil-safe implementation of an interface.
Describe the bug When using an annotated return type of the function along with optional tag, the tagged parameter is not nil in a
if b.Client == nil
comparisonTo Reproduce An example program:
Expected behavior I'd expect to see a message similar to:
instead of
Additional context Add any other context about the problem here.