Is it possible to work with eval(:(x=10)) without touching a global/modules scope


is there a way to make this code work without messing up the global namespace?

x = 10
function f()
  eval(:(local x = 20))
  return eval(:x)
@assert f() == 20
@assert x == 10

This is a minimal example for my real problem. Basically, I want to parse some TOML files and allow the user to refer to do stuff like this:

x = "rand()"
y = "cell.x + rand()"

I figured out a nice way to do that, with the only problem that it messes with the global (or a modules) variables.

It is a bit unclear what you want to do but you cannot dynamically introduce local variables (with e.g. eval) in any way.

1 Like

Thanks for the reply. That answers my question!

I will think about other ways to solve my original problem and then post a separate question with more info.

I found a solution for my problem using regular replace(…). (Should have kept it simple from the beginning :smiley: )

1 Like