traefik / yaegi

Yaegi is Another Elegant Go Interpreter
https://pkg.go.dev/github.com/traefik/yaegi
Apache License 2.0
7.02k stars 349 forks source link

can't implement receiver method #1535

Open introspection3 opened 1 year ago

introspection3 commented 1 year ago

The following program sample.go triggers an unexpected result

package main

import (
    "fmt"
    "reflect"

    "github.com/traefik/yaegi/interp"
)

const src = `package foo
type IWoker2 interface {
    Work(str string) string
}
type Student struct {
}

func (s *Student) Work(str string) string {
    return ("student work" + str)
}
func GetInstance() interface{}{
    return  new(Student)
}
`

func main() {
    i := interp.New(interp.Options{})
    _, err := i.Eval(src)
    if err != nil {
        panic(err)
    }
    v, err := i.Eval("foo.GetInstance()")
    if err != nil {
        panic(err)
    }
    fmt.Println(v.Type().Name())
    w := v.Interface()
    count := reflect.ValueOf(w).NumMethod()
    fmt.Println(count)
}

Expected result

1

Got

0

Yaegi Version

v0.15.1

Additional Notes

too sad,sir....

mvertes commented 1 year ago

This is a known limitation. Methods are emulated in yaegi, because Go reflect doesn't implement the creation of interfaces or method values at runtime (golang/go#16522).