The recent changes that added some redeclaration checks implicitly added more strictness related to namespaces and scopes which, among other things, broke some uses that "accidentally" used to work. For example, given const script1 = ` import "fmt" // more code ` const script2 = ` import "fmt" // some other code ` If one Evals script1, then script2, with the same interpreter, without specifying any scope, as the two fragments would be considered part of the same (.go file) scope by default, a redeclaration error would be triggered because import "fmt" is seen twice. A work-around would have been to specify (a different) i.Name before each Eval call, so that each script is considered as coming from a different .go file, and hence are respectively in different scopes with respect to imports. That lead us to realize we had to make specifying things such as file-scope, and "incremental mode" (aka REPL), more obvious in the context of an Eval call. In addition, we want to lay down the foundations for Yaegi being able to behave more like the go tool wrt to various inputs, i.e. it should be able to take a package directory, or an import path, as input, instead of just a .go file. Hence the introduction of a new kind of Eval method (whose signature is not fixed yet): func (interp *Interpreter) EvalPath(path string) (res reflect.Value, err error) It partially solves the problem described above because: 1. the path given to EvalPath can be used as the file-scope hint mentioned above, for now (even though the related implementation details might change). 2. Eval always runs in incremental mode, whereas EvalPath always runs in non-incremental mode, hence clarifying the situation in that respect. And to avoid confusion, the Name field of Interpreter is now non-exported, since it is somewhat redundant with the path argument of EvalPath. Note that #731 is not fully fixed (and might never be), as a requirement of the proposed solution is to move the input bits of code into respective files (instead of leaving them as strings). Finally, some related bugfixes, documention changes, and some refactoring have been included. Notably, there is no "empty scope" anymore, i.e. name defaults to "_.go" when it is not specified. Updates #731 Fixes #778 Fixes #798 Fixes #789 Co-authored-by: Marc Vertes <mvertes@free.fr>
14 lines
187 B
Go
14 lines
187 B
Go
package main
|
|
|
|
import (
|
|
"guthib.com/toto" // pkg name is actually titi
|
|
)
|
|
|
|
func main() {
|
|
println("Hello", titi.Quux())
|
|
}
|
|
|
|
// GOPATH:testdata/redeclaration-global7
|
|
// Output:
|
|
// Hello bar
|