# eval(x::Expr) has type ::Any

**URL:** <https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523>\
**Category:** New to Julia\
**Created:** [January 16, 2017, 5:25pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523 "2017-01-16T17:25:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [January 16, 2017, 5:25pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/1 "2017-01-16T17:25:11Z")

</div>

I have the following example:

```julia
function create_simple_function()
    str = "v -> v[1]^2*v[3]^-1"
    expr::Expr = parse(str)
    f::Function = eval(expr)
    return f
end
@code_warntype(create_simple_function())

Variables:
  #self#::#create_simple_function
  str::String
  expr::Expr
  f::F

Body:
  begin 
      str::String = "v -> v[1]^2*v[3]^-1" # line 3:
      SSAValue(0) = $(Expr(:invoke, LambdaInfo for #parse#311(::Bool, ::Function, ::String), :(Base.#parse#311), true, :(Main.parse), :(str)))
      expr::Expr = (Base.convert)(Main.Expr,SSAValue(0))::Expr # line 4:
      SSAValue(1) = (Core.eval)(Core.Main,expr::Expr)::Any
      f::F = (Base.convert)(Main.Function,SSAValue(1))::F # line 5:
      return f::F
  end::F

```

My question is : why is `SSAValue(1)` of type `Any`?

In my working code I have thousands of functions to be created this way (with `1` and `2` replaced by `i`s and `j`s etc) and I found by profiling that this line is taking the most time.

Is this the way I should be creating those functions anyway?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 16, 2017, 5:32pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/2 "2017-01-16T17:32:40Z")

</div>

You usually shouldn’t create functions this way. Even if you have to for some reason, you must not create it by parsing a string unless the string is user input. You’ll also get an error on master when you call the function. It’s unclear what you actually want to do so it’s hard to say what’s the right solution for you.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [January 16, 2017, 5:47pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/3 "2017-01-16T17:47:40Z")

</div>

I’m trying to implement Automorphism Groups of Groups. For abelian groups these are given by `GL(N,ZZ)`, so I can get away with matrices; For more general groups I need a generic object/function mapping one generating set to another, e.g. if `G = <x,y| some relations>` automorphism may be given by prescribing its values on the generators, say `(x→x*y, y→x)`.  
If I want to compose two of such automorphisms then – I need to compose those functions.

so If a user gave me input `(x→x*y, y→x)` I want to create a function  
`(x,y) = v → (v[1]*v[2], v[1]) = (x*y, x)`

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 16, 2017, 5:57pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/4 "2017-01-16T17:57:33Z")

</div>

```julia
g(f1, f2) = v->(f1(v[1]), f2(v[2]))

```

??

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [January 16, 2017, 6:04pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/5 "2017-01-16T18:04:52Z")

</div>

maybe I don’t quite understand what You suggest…

I gave only a minimal example in the working code this is what I am doing: first generating `f1` and `f2` then returning the vector of their values. Side-note: `f1` needs the whole `v`;

The thing is – Automorphism Groups are large and I need to generate many of simple functions like those, not a single one.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 16, 2017, 6:11pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/6 "2017-01-16T18:11:58Z")

</div>

The function I give should do

> [@abulak](#):
>
> so If a user gave me input (x→x\*y, y→x) I want to create a function(x,y) = v → (v[1]_v[2], v[1]) = (x_y, x)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 16, 2017, 6:13pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/7 "2017-01-16T18:13:40Z")

</div>

> [@abulak](#):
>
> Side-note: f1 needs the whole v;

So it’s even simpler.

```julia
g(f1, f2) = v->(f1(v), f2(v))

```

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [January 16, 2017, 6:27pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/8 "2017-01-16T18:27:44Z")

</div>

Sure, and this is exactly what I am doing;-)

Now if You look at automorphisms of a group with 10 (or more) generators It contains at least hundreds similar automorphisms (ones comming from multiplication on the left/right by a generator/its inverse) do You have to write all of them by hand as You suggested? I thought: Metaprogramming, i.e. make julia generate code for me.

hence the function in the first post (which is just one of `fi`s) should probably be like

```julia
 function create_automorphism(i,j)
    str = "v -> v[i]*v[j]"
    expr::Expr = parse(str)
    f::Function = eval(expr)
    return f
end

```

Now, when it should be somehow clear what I want to do, the question remains:  
Why is eval `::Any` and is this the way to go?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 16, 2017, 6:37pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/9 "2017-01-16T18:37:24Z")

</div>

Julia performs type inference based purely upon the _types_ of the variables in your function. In that function, you take a string and evaluate it. That string could contain _anything_, including `run(`rm -rf /`)`, so it could similarly return anything.

Just return the function directly. This returns a closure and holds on to `i` and `j` for you:

```nohighlight
julia> function create_automorphism(i,j)
            return v->v[i]*v[j]
       end
create_automorphism (generic function with 1 method)

julia> f = create_automorphism(2,3)
(::#5) (generic function with 1 method)

julia> f(1:3)
6

```

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [January 16, 2017, 6:51pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/10 "2017-01-16T18:51:39Z")

</div>

Thanks! for generators this should do;

If the user supplied me a different automorphism (with many operations to do) I should probably do something like this:

```julia
ex = :(GroupIdentity())
for (idx,power) in table
    ex = :($ex * v[$idx]^$power)
end
return eval(:(v -> $ex))

```

operating directly on the level of expressions this will save me `parse` operation.  
eval is still there.

Probably I wanted to be too generic…

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 16, 2017, 7:16pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/11 "2017-01-16T19:16:02Z")

</div>

Again, you don’t need to be using eval in this case.

```nohighlight
julia> function create_automorphism(op, table)
            return v->(op * prod(v[idx]^power for (idx, power) in table))
       end

julia> f = create_automorphism(1, ((1,1), (2,2), (3,3)))
(::#5) (generic function with 1 method)

julia> f([.1,.2,.3])
0.00010800000000000002

julia> .1 * .2^2 * .3^3
0.00010800000000000002

```

This could have just as easily been `create_automorphism(GroupIdentity(), table)`.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [January 16, 2017, 7:18pm UTC](https://discourse.julialang.org/t/eval-x-expr-has-type-any/1523/12 "2017-01-16T19:18:11Z")

</div>

@mbauman Thanks a lot!

So obvious when I look at it now…
