reflect.Type from type literal

153 views
Skip to first unread message

hector.j...@gmail.com

unread,
Sep 22, 2016, 12:14:55 PM9/22/16
to golang-dev
Hi!

First time on the mailing-list for me, please pardon me if I am not doing this correctly.

I wanted to know if it would be possible to consider allowing to get a reflect.Type simply via the type name (not as a string value, but as a literal).

package main


import (
 
"fmt"
 
"reflect"
)


type myInterface
interface{}


func main
() {
 
var tUint reflect.Type = reflect.TypeOf(uint(0)) // current way to get a reflect.Type: create a dummy value of this type
 
//var tUint reflect.Type = typeReflection(uint) // what I would like to have: a built-in func, with this signature: func typeReflection(Type) reflect.Type
 
//var tUint reflect.Type = uint // or maybe just reference the type directly


 
var tInterface reflect.Type = reflect.TypeOf((*myInterface)(nil)).Elem() // even more complicated for an interface, this isn't pretty nor intuitive
 
//tInterface := typeReflection(myInterface) // that's a lot clearer. the func name maybe isn't the best possible, but that's a detail that can be changed
 
//tInterface := myInterface
 fmt
.Printf("Hello, playground. %q %q\n", tUint.String(), tInterface.String())
}




In addition to making my life easier (I'm working on some code generation tool), I feel like this would make sense.


What do you think?

Brad Fitzpatrick

unread,
Sep 22, 2016, 12:17:03 PM9/22/16
to hector.j...@gmail.com, golang-dev
This would be a language change, and the language is basically frozen.


--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages