Task

Create a variable with a user-defined name.

The variable name should not be written in the program text, but should be taken from the user dynamically.



See also
   Eval in environment is a similar task.


def makeNounExpr := <elang:evm.makeNounExpr>
 
def dynVarName(name) {
    def variable := makeNounExpr(null, name, null)
 
    return e`{
 
        def a := 1
        def b := 2
        def c := 3
 
        {
            def $variable := "BOO!"
            [a, b, c]
        }
 
    }`.eval(safeScope)
}
 
? dynVarName("foo")
# value: [1, 2, 3]
 
? dynVarName("b")
# value: [1, "BOO!", 3]
 
? dynVarName("c")
# value: [1, 2, "BOO!"]