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: Go 2: reflect.Type literals #27231
Comments
Not sure IIUC: https://play.golang.org/p/cCYeXm17cRl ? |
It doesn't work for all types: https://play.golang.org/p/umwP9U8tsqn. The pattern I gave initially seam to work for all types. |
Sure, but you do know in advance what type T is, don't you? Anyway, one can still do https://play.golang.org/p/NgMk4-6b2Og and it works for both scalar and non-scalar types. |
Obviously, we can do that. But the goal is to get a reflect.Type value without having to declare a useless value. We need to be able to write something like t := reflect.Type(int) or t := reflect.Type(anyType). We need this when we want to pass the type value as argument to a function for instance. It is dumb to declare a variable we won't use just to get the type value. The expression I gave returns the type value without needing to declare a variable. It's just cumbersome to use and read. |
The "useless" variable is used to achive your goal ;-)
So far only some builtin functions can take a type as their first argument. Allowing that for user code in a package is a big language change. You'd need to make this proposal much more complete for such change to be even considered.
If you pass - as you call it - a 'type value' to a function, the variable that you can directly pass to func f(x interface{}) reflect.Type { return reflect.TypeOf(x) } edit: grammar |
What I mean by "type value" is a value of type reflect.Type. Currently I defined my function as Your last suggestion is much more elegant. Here is an example: https://play.golang.org/p/jrqOUu5bZD4 My goal was simply to report an issue to eventually clean up in Go2. I don't have a solution to suggest. As I said, I suspect the solution might be linked to the generic system where a function may be parameterized by a type. |
How often do these expressions occur in real code? Is it really worth changing the language for this? |
My point would probably be more clear if the title was :
I can't say how often the My goal was only to add a radar blip on this issue for the Go 2 developer team so they can fix it if it's easy. It might come for free with generics. Who can say now ? A typical use case is when we need to build a map with Go types as keys and various kind of values, like an encoding function or specific actions for instance. The actual different expressions to get the equivalent of a type literal are not simple and readable. |
The contracts proposal would provide a way to express this using type parameters (as @chmike hinted at above): func TypeOf2(type T)() reflect.Type {
return reflect.TypeOf((*T)(nil)).Elem()
} With usage as: t := reflect.TypeOf2(io.Reader)() (Of course |
It's fairly common to use this pattern since it is one of the few ways to get a |
As @dsnet says, we should be able to write this function using generics. Let's go with that. We can reopen this if no generics proposal is adopted. |
Currently, to get a reflect.Type value for a type
T
, one has to use the following expressionIt would be nice if we could have a lighter and more readable expression to get the reflect.Type value of a type. Maybe generics can help, if they are added to Go v2.
The text was updated successfully, but these errors were encountered: