-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: reflect: add generic variants of TypeOf()
and ValueOf()
#50741
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
Comments
While getting the |
Perhaps not. I know that I found the behavior of the existing functions around interfaces somewhat confusing when I was first getting started with Go, and I'm sure that someone else has as well. Also, as I mentioned, the examples in the proposal are some of the simpler cases to work around, but cases where the values aren't addressable are slightly more awkward. Edit: While it's quite possible to implement this functionality externally ( |
This proposal has been added to the active column of the proposals project |
It's maybe not the common case, but for Speaking for myself, I need to do that every time I want to compare a reflected-on type with some known With regard to When calling So with respect to this proposal, I support only the variant suggested in the addendum of the proposal description. I've already been defining this function myself and found it pleasant and clear to use:
For the record, another function I've found useful has been this one:
This avoids the awkwardness needed to extract a reflect.Value A suitable spelling for the Some possibilities and their drawbacks:
|
The two that @rogpeppe points out seem plausible, but it seems like we should wait on adding generics still. |
Placed on hold. |
I'm not entirely sure it would help much. In my experience, reflect.ValueOf is most often called on a value of interface type, and in that case, the value returned by a generic version of reflect.ValueOf would always have a Type.Kind of Interface, and so in practice you'd just end up calling IsNil to check if it's OK to call Elem (because you're almost always interested in what's going on inside the interface value that's passed to ValueOf), which I think amounts to much the same thing in the end as checking for nil on the original interface value. |
I think this should come off hold and be merged with #60088. |
This proposal is a duplicate of a previously discussed proposal, as noted above, |
I think the auto-message is wrong. It's a duplicate of a newer issue, not a previously discussed proposal. Anyway, seems fine to move all discussion to #60088. |
The current implementations of
reflect.TypeOf()
andreflect.ValueOf()
each take anany
for the value about which they are retrieving information. This works just fine in most cases, but can also cause annoyances when trying to get reflection info about an interface type itself. Due to the way interfaces work, the functions never even see the original interface type unless it's inside of a structured type of some kind, such as a poitner. For example,This problem is exacerbated in the case where the value in question is from a non-addressable source, such as a function return, requiring an extra variable just to be able to address the interface type.
This is not true for generic functions, however. As such, I propose adding two new functions which I will call
GenericTypeOf()
andGenericValueOf()
for lack of a better name. Feel free to bikeshed.These functions behave exactly as the existing two do, but with the difference that when passed a value of an interface type, they operate directly on the compile-time interface type, rather than the underlying runtime type.
Addendum
As an aside, a
reflect.TypeOf()
variant that only takes a type and not a value, i.e.func OnlyTypeOf[T any]() reflect.Type
, might be useful as well for the case where you need to perform reflection on a type that you know for sure at compile-time, such as when you need to store thereflect.Type
representing something to check other values against it later. One example that comes to mind is a reflection-based serialization scheme that has special handling fortime.Time
. The implementation could just dovar timeType = reflect.OnlyTypeOf[time.Time]()
somewhere in the global scope instead of having to do the somewhat less elegantvar timeType = reflect.TypeOf(time.Time{})
. This didn't seem useful enough to warrant mentioning in the main part of the proposal, however.The text was updated successfully, but these errors were encountered: