Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Howto assign a variable or value to yaegi? #1542

Open
andig opened this issue Apr 15, 2023 · 2 comments
Open

Howto assign a variable or value to yaegi? #1542

andig opened this issue Apr 15, 2023 · 2 comments
Labels
area/api change in API enhancement New feature or request proposal

Comments

@andig
Copy link

andig commented Apr 15, 2023

Proposal

First, please excuse me for asking here. https://community.traefik.io/c/yaegi seems a dead link and the community page doesn't have a yaegi section. Please close if inappropriate.

Background

I'd like to

a := "foo"
interp.Set("pkg.Vars.B", a)

instead of

interp.Eval("pkg.Vars.B := \"foo\"")

which requires individual handling per type. Set could follow Go's assignability rules.

Workarounds

Use Eval()

@mvertes mvertes added enhancement New feature or request area/api change in API proposal labels Apr 20, 2023
@andig
Copy link
Author

andig commented Oct 30, 2024

What would it take to implement something like this? While Evaling is straightforward for simple types it gets much more ugly and may require additional parsing with e.g. assigning a time.Time. It would be really nice to do this from an already initialised time.

@andig
Copy link
Author

andig commented Nov 23, 2024

Just stumbled again across

if _, err := vm.Eval(fmt.Sprintf("ts = time.Unix(%d, 0)", ts.Unix())); err != nil {
	return 0, err
}

which could become

interp.Set("pkg.Vars.ts", ts)

and save the round trip. I've looked at the yaegi sources but couldn't find a good starting place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api change in API enhancement New feature or request proposal
Projects
None yet
Development

No branches or pull requests

2 participants