2

Is it possible to evalute a string to an expression containing a string ? To give a mwe :

eval(Meta.parse("x = 2"))

is evaluted to:

x = 2

But I would like x to be a string itself. My naive try in making x a string:

eval(Meta.parse("x = "2" "))

which i would like to be evaluted to

x = "2"

gives me an error. Therefore, my question is: Is it possible to do this, using only strings ?

2 Answers 2

2

You need to escape the quotation marks:

eval(Meta.parse("x = \"2\""))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! This is exactly what i was searching for :)
2

@August has answered the question, however just one another way usually more practical when doing metaprogramming:

julia> eval(:(x= "2"));

julia> x
"2"

This nicely works with interpolation:

julia> z="hello";

julia> eval(:(x2 = $z));

julia> x2
"hello"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.