# Correctly generate function from user input

**URL:** <https://discourse.julialang.org/t/correctly-generate-function-from-user-input/29214>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 27, 2019, 3:02am UTC](https://discourse.julialang.org/t/correctly-generate-function-from-user-input/29214 "2019-09-27T03:02:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ry-dgel](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@ry-dgel](https://discourse.julialang.org/u/ry-dgel)\
**Post date:** [September 27, 2019, 3:07am UTC](https://discourse.julialang.org/t/correctly-generate-function-from-user-input/29214/2 "2019-09-27T03:07:54Z")

</div>

I’ve already read through the following threads, and while giving solid advice, I feel that they don’t entirely apply to my situation, and have yet to tease out a working solution from the information they provide…

> <https://stackoverflow.com/questions/34016768/julia-invoke-a-function-by-a-given-string>

> [@How do I create a function from an expression](https://discourse.julialang.org/t/how-do-i-create-a-function-from-an-expression/12349):
>
> Suppose I have expr = :(sin(x)+cos(x)) How can I get a function looking like x-\>sin(x)+cos(x) I have tried to write such a macro macro expr2fn(expr, arg) return :($arg -\> $(expr)) end but it does not work julia\> f = @expr2fn expr x (::#63) (generic function with 1 method) julia\> f(1.0) :(sin(x) + cos(x)) Not sure what is happening here.

> [@Parsed anonymous functions, type inference, and Base.invokelatest](https://discourse.julialang.org/t/parsed-anonymous-functions-type-inference-and-base-invokelatest/6518):
>
> I have a situation where at some point during runtime a string is parsed and eval’d to an anonymous function, e.g. fa = eval(parse("x -\> mean(x)")) Using Julia v0.5, I could then call fa([1,2,3]) later in the same routine without a problem. In v0.6 however, such a call results in a MethodError mentioning the “world age”. Reading the docs suggests using: Base.invokelatest(fa, [1,2,3]) which works, but, according to the docstrings for invokelatest the type of the output from from the call to f…

---

_[View the full topic](https://discourse.julialang.org/t/correctly-generate-function-from-user-input/29214)._
