-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
reflect: when MakeFunc returns, assign its results into the desired types #28761
Comments
https://golang.org/pkg/reflect/#ValueOf
I personally think that it's intuitive, it's returning the concrete value. I think it also aligns well with how interfaces work (that interfaces should be made up of a concrete type and value).
I personally don't think it should do any implicit type conversions. It would have no way of knowing what we want to return, for instance if I want an A few solutions -
|
It knows that, it was provided as the first argument to |
If the result is assignable to the type we need, then yes I think it makes sense to convert it. |
Change https://golang.org/cl/174531 mentions this issue: |
This fails with a run-time panic:
It's quite confusing, as
io.EOF
is anerror
, yet it can't be returned asreflect.ValueOf(io.EOF)
in a slot that needs anerror
. This happens because the "error"ness is lost, and reflect sees only the concrete type*errors.errorString
.To fix it, change
reflect.ValueOf(io.EOF)
toreflect.ValueOf(&io.EOF).Elem()
. Kinda hacky, but it does work.Should we instead upgrade the reflect package to do conversions when assigning the return values? We'd only do conversions that would be done during assignments under the language spec. Since there is nothing untyped at runtime, this would just be unnamed->named types, concrete->interface and interface->interface conversions, and bidirectional->unidirectional channels.
(Are we allowed to convert something that used to panic into something that now works?)
#6871 was a similar proposal that was rejected.
The text was updated successfully, but these errors were encountered: