# Evaluation into closed module - what's wrong?

**URL:** <https://discourse.julialang.org/t/evaluation-into-closed-module-whats-wrong/114526>\
**Category:** General Usage\
**Tags:** metaprogramming, eval\
**Created:** [May 21, 2024, 6:45pm UTC](https://discourse.julialang.org/t/evaluation-into-closed-module-whats-wrong/114526 "2024-05-21T18:45:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 21, 2024, 6:45pm UTC](https://discourse.julialang.org/t/evaluation-into-closed-module-whats-wrong/114526/1 "2024-05-21T18:45:56Z")

</div>

Hi metaprogramming afficionados,

The package [SparseConnectivityTracer.jl](https://github.com/adrhill/SparseConnectivityTracer.jl) works by operator overloading, like ForwardDiff.jl or Measurements.jl. We overload operations on new number types called `ConnectivityTracer`, `GradientTracer` and `HessianTracer`, in order to track dependencies of a function’s outputs on its inputs.

I am currently trying to support SpecialFunctions.jl with a package extension, but the tests fail with the following error:

```julia
ERROR: LoadError: InitError: Evaluation into the closed module 
`SparseConnectivityTracer` breaks incremental compilation because
the side effects will not be permanent. This is likely due to some
other module mutating `SparseConnectivityTracer` with `eval`
during precompilation - don't do this.

```

I don’t understand this error message, and I’m a real noob at metaprogramming. My best guess is that it is related to me using `@eval` inside functions.

Would anyone be so kind as to take a look at the PR and try to figure out what I did wrong? I added more details in the PR description

> <https://github.com/adrhill/SparseConnectivityTracer.jl/pull/82>
>
> At the moment, the tests fail because I try to "\`eval\` into a closed module duri…ng precompilation".
> 
> For anyone who wants to take a look:
> \- the new function \`overload\_all\` calls multiple functions that define specific overloads with \`@eval\`
> \- the typical logic for one of these functions can be seen here:
> https://github.com/adrhill/SparseConnectivityTracer.jl/blob/c49dc3293941b39615d8b31e5def82f57e537d22/src/overload\_connectivity.jl#L13-L24
> 
> I would love some guidance on:
> \- whether this approach even makes sense
> \- when to run the \`eval\`s (during \`init\` or during precompilation)
> \- where I should use the macro \`@eval\` or the function \`eval\`
> \- in which module I should \`eval\`

Related topic, which did not fix my issue:

> [@Use of eval in closed module and "incremental compilation may be broken"](https://discourse.julialang.org/t/use-of-eval-in-closed-module-and-incremental-compilation-may-be-broken/30028/6):
>
> So if anyone should read this question in the future, I finally found out that the problem was to specify properly where the eval happens (and yes you actually have to use eval here because the emitted code has to be from A not B) Anyway so calling parentmodule(T).eval solves the problem, it took me some time to find it but it works perfectly for my use case sweat_smile

---

<div class="post-metadata">

**Author:** ![kdheepak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdheepak/32/10881_2.png) [@kdheepak](https://discourse.julialang.org/u/kdheepak)\
**Post date:** [May 21, 2024, 9:12pm UTC](https://discourse.julialang.org/t/evaluation-into-closed-module-whats-wrong/114526/2 "2024-05-21T21:12:05Z")

</div>

Have you tried `@eval $ms function $fns(...) ... end` instead of `@eval function $ms.$fns(...) ... end`?

From the docs (emphasis mine):

> help?\> @eval  
> @eval [mod,] ex
> 
> Evaluate an expression with values interpolated into it using eval. **If two arguments are provided, the first is the module to evaluate in.**

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 22, 2024, 12:58am UTC](https://discourse.julialang.org/t/evaluation-into-closed-module-whats-wrong/114526/3 "2024-05-22T00:58:52Z")

</div>

The issue is not your evals. Rather you are calling a function during the extension’s init which calls `@eval` within SCT.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 22, 2024, 5:05am UTC](https://discourse.julialang.org/t/evaluation-into-closed-module-whats-wrong/114526/4 "2024-05-22T05:05:26Z")

</div>

> [@mkitti](#):
>
> Rather you are calling a function during the extension’s init which calls `@eval` within SCT.

Within which module should i call `@eval` then? `Base`/`SpecialFunctions`?

It doesn’t seem possible to `@eval` into the calling scope itself 🤔

> [@Evaluation in calling scope?](https://discourse.julialang.org/t/evaluation-in-calling-scope/34897/2):
>
> There are probably workarounds to achieve what you want to achieve but the straight-forward answer is just that you cannot eval in calling scope since eval evaluates in global scope.
