# Eval in Turing model

**URL:** <https://discourse.julialang.org/t/eval-in-turing-model/132397>\
**Category:** General Usage\
**Tags:** turing\
**Created:** [September 15, 2025, 12:56pm UTC](https://discourse.julialang.org/t/eval-in-turing-model/132397 "2025-09-15T12:56:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyan](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cyan](https://discourse.julialang.org/u/Cyan)\
**Post date:** [September 15, 2025, 12:56pm UTC](https://discourse.julialang.org/t/eval-in-turing-model/132397/1 "2025-09-15T12:56:59Z")

</div>

Hi, all. I am confusing if I use eval function like

```julia-auto
eval(Meta.parse("sigma ~ Normal(0,1)"))

```

does it will affect the speed of model?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 16, 2025, 9:44am UTC](https://discourse.julialang.org/t/eval-in-turing-model/132397/2 "2025-09-16T09:44:00Z")

</div>

If I understand your question correctly, then the answer is no. The resulting model does not know how it was constructed.  
However, before construction you have additional overhead of `Meta.parse` and `eval` of course. Additionally, the latter means that Julia cannot infer the type so further use of the resulting model is type unstable and incurs dynamic dispatches (which you could restrict using a [function barrier](https://docs.julialang.org/en/v1/manual/performance-tips/#kernel-functions)).

But more likely than not you don’t need `eval(Meta.parse(..))`. Would you mind explaining why you think you need this?

---

<div class="post-metadata">

**Author:** ![penelopeysm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/penelopeysm/32/213172_2.png) [@penelopeysm](https://discourse.julialang.org/u/penelopeysm)\
**Post date:** [September 16, 2025, 2:40pm UTC](https://discourse.julialang.org/t/eval-in-turing-model/132397/3 "2025-09-16T14:40:58Z")

</div>

It doesn’t work anyway:

```julia
julia> using Turing

julia> @model function f()
           eval(Meta.parse("sigma ~ Normal()"))
       end
f (generic function with 2 methods)

julia> f()()
ERROR: UndefVarError: `sigma` not defined in `Main`
Suggestion: check for spelling errors or missing imports.

```

The model macro acts on the contents before `eval` and `parse` can get to it.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 16, 2025, 3:10pm UTC](https://discourse.julialang.org/t/eval-in-turing-model/132397/4 "2025-09-16T15:10:21Z")

</div>

I am a bit confused how your post relates to OP and I am no expert in Turing but that’s not how I interpret the error message. You see `eval` evaluates the expression in _global scope_ and there is no variable `sigma` in global scope. I am fairly sure that the macro `@model` does not reach inside the `eval` and does not transform the expression and that’s why you are seeing the error.

If these two posts belong to the same underlying problem then you should try to explain that in order to get suitable help.

---

<div class="post-metadata">

**Author:** ![penelopeysm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/penelopeysm/32/213172_2.png) [@penelopeysm](https://discourse.julialang.org/u/penelopeysm)\
**Post date:** [September 16, 2025, 11:08pm UTC](https://discourse.julialang.org/t/eval-in-turing-model/132397/5 "2025-09-16T23:08:52Z")

</div>

> [@abraemer](#):
>
> I am a bit confused how your post relates to OP

OP is asking whether using `eval(Meta.parse(...))` in a Turing model makes it slower. My point is that they can’t do that, it will error, so the question of whether it’s faster or not is not well-founded.

> [@abraemer](#):
>
> that’s not how I interpret the error message […]

Yes, you’re saying the same thing as me (although I was admittedly much more terse and less helpful). `@model` does not transform the string `"sigma ~ ..."` but that’s precisely because the contents of `@model ...` are expanded, before `eval(parse(...))` which is only executed at runtime. Consequently attempting to run `sigma ~ ...` at runtime then gives the error as shown.
