Home > Blockchain >  Get type parameter from a generic struct using reflection
Get type parameter from a generic struct using reflection

Time:09-27

Imagine I have the following struct:

type MyGeneric[T string | int] struct {
}

I want to check whether the generic used to instantiate that struct was a string or a int when creating a new MyGeneric.

myGenericString := MyGeneric[string]{}
myGenericString.canHandle("hello") -> should return true
myGenericString.canHandle(8) -> should return false

func (mG MyGeneric[T]) canHandle(value any) bool {
    // how to get what T is the same type as value
}

CodePudding user response:

It hasn't been implemented yet. There is an open proposal about adding the necessary methods to reflect.Type.

The current workaround as of Go 1.19 is to parse the string obtained from TypeOf. Something like this:

var r = regexp.MustCompile("[A-Za-z0-9_] \\.[A-Za-z0-9_] \\[(.*)\\]")

func (mG MyGeneric[T]) canHandle(value any) {
    tname := reflect.TypeOf(mG).String() // this is `main.MyGeneric[string]`
    match := r.FindStringSubmatch(tname)
    fmt.Println(match[1]) // string
}

CodePudding user response:

Another solution is to add a placeHolder field to your struct, that can be used to get its type via type switch to avoid reflect:

type MyGeneric[T string | int] struct {
  placeHolder T
}

func (mG MyGeneric[T]) canHandle(value any) bool {
    switch t1 := any(p.placeHolder).(type) {
    case any:
        switch t2 := value.(type) {
        case any:
            return t1 == t2
        }
    }
    return false
}
  • Related