traefik / yaegi

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

import generic functions from extracted Go code as interpreted code #1647

Open rcoreilly opened 1 month ago

rcoreilly commented 1 month ago

This PR fixes issues like #1457 #1601 by importing the source code for generic functions in extracted Go code, so that it can be interpreted in yaegi.

You need to add a //yaegi:add comment directive to functions to so import, as importing all generic functions might not be desirable or feasible. In particular, given the need to interpret the generic code, it can not access any non-exported functions, types, or fields.

The extracted code is wrapped in a GenericFunc string type value as below:

    err := i.Use(Exports{
        "guthib.com/generic/generic": map[string]reflect.Value{
            "Hello": reflect.ValueOf(GenericFunc("func Hello[T any](v T) { println(v) }")),
        },
    })

This PR builds on #1642 which interacts with this code in extracting the function source.

rcoreilly commented 1 month ago

This and all the other PRs from me should now be good in terms of passing tests and working in our "real world" codebase which uses a lot of generics.