-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
Is it true that this library doesn't use the reflection package? You lied. #30
Comments
It's this kind of PR (title, content) that drives people away from open source contributions. Please consider the possibility that the author didn't deliberately lie. It would have been approximately 1000x friendlier and more useful to have said "hey, |
Is there no way to use the full path to the package from |
I totally agree with @kentquirk. If someone else gets curious about the details of how it's using reflection (like I was), there's the piece of code (from |
There actually is a trick you can use to avoid reflection on map/context keys and it's called 0-width generic structs. type key[T any] struct{} Instances of such structs compare unequal if their T differs, such that I would like to implement it and open a PR, but these reflected names are used as part of package public api (see |
@btvoidx: Please implement Minimal/global/simplified version could look like this: package do
import (
"errors"
)
type key[T any] struct{ name string }
type provider[T any] func() (T, error)
var services = make(map[any]any)
func ProvideNamed[T any](name string, p provider[T]) {
services[key[T]{name}] = p
}
func ProvideNamedValue[T any](name string, v T) {
services[key[T]{name}] = v
}
func InvokeNamed[T any](name string) (T, error) {
provider, ok := services[key[T]{name}].(provider[T])
if ok {
return provider()
}
value, ok := services[key[T]{name}].(T)
if ok {
return value, nil
}
return empty[T](), errors.New("not found")
}
func Provide[T any](p provider[T]) {
ProvideNamed[T]("", p)
}
func ProvideValue[T any](v T) {
ProvideNamedValue[T]("", v)
}
func Invoke[T any]() (T, error) {
return InvokeNamed[T]("")
}
func empty[T any]() (t T) {
return
} |
I think it is rather dishonest to still claim in the README to not use reflection when it has been pointed out that it does in fact use it. |
I thought there was some new hack to implement dependency injection without reflection and only through generics. A quick look at the code revealed that this was a lie.
The text was updated successfully, but these errors were encountered: