How do you evaluate a parameter/variable if you are not in Main eval or Core.Eval do not work?

Hello, I would really appreciate your help with the following problem. I will want to learn how evaluate a variable outside of Main, and I could not find a way to do that yet.

Expected: Eval will work for #println(Core.eval(Main, Symbol(param))).

Instead: Error parameter is not defined any of the one in the list, but it is defined because I can print it no problem. println(parameter1) or pepe1. Please, see code below, thank you.

I tried eval before in Main and it works ( and it works meaning not in the function).
May expr (metaprogramming section perhaps the answer? ) I thought of some ways of generating some code like removing the “” using RegExp, and using dicitonaries to aid me, but will be much easier to learn how to evaluate inside pepito( the inner function of newfunction.jl. Thank you!

“”" A different .jl file will do this “”"

include(“newfunction.jl”)

pepito= newfunction(1)

pepito(1)

“”" first file will finish here “”"

Expected: Eval will work for #println(Core.eval(Main, Symbol(param))).

Instead: Error parameter is not defined any of the one in the list, while when I do
println(parameter1) or pepe1. I tried eval in Main( and it works meaning not in the function) and it works. So, how you evaluate something when you are not in Main. Is expr (metaprogramming section perhaps the answer? ) I thought of some ways of generating some code like removing the " " using RegExp and generating what I want and then using dicitonaries to check if things are defined etc. But, perhaps either Expr will help or maybe Module.

module amodule
I tried include(“newfunction,jl”)
end

Then in the function I will use eval(Main.amodule, Symbol(param)) # Still does not work ?
Again thank you!!!

“”" THIS IS MY FUNCTION “”" ( newfunction.jl)

function newfunction(input)

parameter1 = input
parameter2 = input + 1
parameter3 = input + 3

“”" parameters “”"
parameters= [“pepe1”, “pepe2”, “pepe3”]

println(parameters)

function pepito2(t)

println(“Starting to print parameter from parent”)
println(parameter1)
println(parameter2)
println(parameter3)
println(“Done”)

pepe1 = parameter1 + 1
pepe2 = parameter2 + 2
pepe3 = parameter3 + 2
z1= pepe1 + pepe2 + pepe3
println(“Now we are printing pepe1”)
println(pepe1)
println(“Now we are printing pepe2”)
println(pepe2)
println(“Now we are printing pepe3”)
println(pepe3)

newdic= Dict()

for param in parameters

if @isdefined(param)==true
    println(param, "is defined")
    #println(Core.eval(Main, Symbol(param))) I tried both.
    println(eval(Symbol(param)))
else

    println("NotDefined")

end

end

“”"
if t!=0

for param in parameters
    if @isdefined(param)==true
        println(param, "is defined")
        println(eval(Symbol(param)))
    else
        println(param)
        println("NotDefined")

    end
end

end

“”"
return z1

end

return pepito2

end

“”“A different .jl file will do this “””

include(“newfunction.jl”)

pepito= newfunction(1)

pepito(1)

Expected: Eval will work for #println(Core.eval(Main, Symbol(param))).

Instead: Error parameter is not defined any of the one in the list, while when I do
println(parameter1) or pepe1. I tried eval in Main( and it works meaning not in the function) and it works. So, how you evaluate something when you are not in Main. Is expr (metaprogramming section perhaps the answer? ) I thought of some ways of generating some code like removing the “” and using dicitonaries to aid me.

“”" THIS IS MY FUNCTION “”" ( newfunction.jl)

function newfunction(input)

parameter1 = input
parameter2 = input + 1
parameter3 = input + 3

“”" parameters “”"
parameters= [“pepe1”, “pepe2”, “pepe3”]

println(parameters)

function pepito2(t)

println(“Starting to print parameter from parent”)
println(parameter1)
println(parameter2)
println(parameter3)
println(“Done”)

pepe1 = parameter1 + 1
pepe2 = parameter2 + 2
pepe3 = parameter3 + 2
z1= pepe1 + pepe2 + pepe3
println(“Now we are printing pepe1”)
println(pepe1)
println(“Now we are printing pepe2”)
println(pepe2)
println(“Now we are printing pepe3”)
println(pepe3)

newdic= Dict()

for param in parameters

if @isdefined(param)==true
    println(param, "is defined")
    #println(Core.eval(Main, Symbol(param))) I tried both.
    println(eval(Symbol(param)))
else

    println("NotDefined")

end

end

“”"
if t!=0

for param in parameters
    if @isdefined(param)==true
        println(param, "is defined")
        println(eval(Symbol(param)))
    else
        println(param)
        println("NotDefined")

    end
end

end

“”"
return z1

end

return pepito2

end


"""A different .jl file will do this """


include("newfunction.jl")

pepito= newfunction(1)

pepito(1)

Expected: Eval will work for  #println(Core.eval(Main, Symbol(param))). 

Instead: Error parameter is not defined any of the one in the list, while when I do 
println(parameter1) or pepe1. I tried eval in Main( and it works meaning not in the function) and it works. So, how you evaluate something when you are not in Main. Is expr (metaprogramming section perhaps the answer? ) I thought of some ways of generating some code like removing the "" and using dicitonaries to aid me. 

"""   THIS IS MY FUNCTION """  ( newfunction.jl)

function newfunction(input)

parameter1 = input
parameter2 = input + 1
parameter3 = input + 3

"""		 parameters      """
parameters= ["pepe1", "pepe2", "pepe3"]

println(parameters)


function pepito2(t)

println("Starting to print parameter from parent")
println(parameter1)
println(parameter2)
println(parameter3)
println("Done")

pepe1 = parameter1 + 1
pepe2 = parameter2 + 2
pepe3 = parameter3 + 2
z1= pepe1 + pepe2 + pepe3
println("Now we are printing pepe1")
println(pepe1)
println("Now we are printing pepe2")
println(pepe2)
println("Now we are printing pepe3")
println(pepe3)

newdic= Dict()


for param in parameters

    if @isdefined(param)==true
        println(param, "is defined")
        #println(Core.eval(Main, Symbol(param))) I tried both.
        println(eval(Symbol(param)))
    else

        println("NotDefined")

    end
end

"""
if t!=0

    for param in parameters
        if @isdefined(param)==true
            println(param, "is defined")
            println(eval(Symbol(param)))
        else
            println(param)
            println("NotDefined")

        end
    end

end

"""
return z1

end

return pepito2 

end

Thank you once more again! I look forward to your response.

FYI I think I said that I did try :
module amodule 
include("newfunction.jl")
end

and tried eval(Main,Symbol(param)). But, I forgot to mention that then I tried to put a new module from the beggining of pepito (inner function) to end , but I got an error that module needs to be at top, so that definitely did not work for me, thanks! 

Christian









I have to admit that I did not read your code since it was not quoted, but I am wondering if you really need to eval here.

What is it that you are trying to do? If you want to pass around parameters, a struct, a Dict, or a NamedTuple are likely to be better solutions.

4 Likes

In Julia (unlike Matlab, Python, and other interpreted languages) you cannot eval inside the scope of a function. (When the function is running it has already been compiled into machine code.)

eval always runs in the global scope (that is: outside of the function) even when called from inside a function.

Inside of a function, you can use the @isdefined macro.

julia> function f()
             println(@isdefined x)
             x = 3
             println(@isdefined x)
         end
  f (generic function with 1 method)
  
  julia> f()
  false
  true
1 Like

Thank you so much Tamas. I actually tried to properly paste my code, but not sure how to do that. I will google it, is there a way to upload them?

I want to really check the value of parameters and if they do not meet a certain criteria save those. I have a list of the parameters to check, so if I remove the function definition portion, I can use eval to evaluate eval(Symbol(“string”)) , but inside the function I am not able to do this. So, if I do a for loop and do
for param in parameters
dictionary[param]= param (but without " " )
end

Let me know if more clear, and thanks I will explore more about struct thanks. Perhaps I can transform the list to a struct definition and save them that way, as with dictionaries will be more troublesome to do. Thanks

Thank you so much Per for your help. This really helped me , I do use @isdefined is handy, thanks, that is too bad there is no way to evaluate inside a function, but I understand from your answer that is has something to do with the compilation.

It is interesting to me that we can check if they are defined , but we cannot evaluate it, I will learn more about macros , thanks for the help ! I will try to do what I am trying to do in some other way! thanks