traefik / yaegi

Yaegi is Another Elegant Go Interpreter

Home Page:https://pkg.go.dev/github.com/traefik/yaegi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Import third party dependencies which are implemented with golang generic type

nyumaple07 opened this issue · comments

Proposal

Hi there,

Can we have support for adding third party dependencies which are implemented using golang generic type to an interpreter?

Appreciate any comments!

Thanks,

Background

Let's have an example here:

type Integer[T int32 | int64] struct {
     someAttribute T
}

When I tried to add it to an interpreter, I had to add an actual type(which is Integer[int32] or Integer[int64]). Using that way, I can not use anything like Integer[int32] or Integer[int64] inside my script.

interpreter.Use(
map[string]map[string]reflect.Value{
    "xx/xx": {
    "Integer": reflect.ValueOf((*validation.Integer[int32])(nil)),
    },
})

Workarounds

One workaround is to rename those types.

type Integer32 Integer[int32]
type Integer64 Integer[int64]

interpreter.Use(
map[string]map[string]reflect.Value{
    "xx/xx": {
    "Integer32": reflect.ValueOf((*validation.Integer[int32])(nil)),
    "Integer64": reflect.ValueOf((*validation.Integer[int64])(nil)),
    },
})