# What's a good way to add method to \`Base.MainInclude.eval\`?

**URL:** <https://discourse.julialang.org/t/whats-a-good-way-to-add-method-to-base-maininclude-eval/91697>\
**Category:** General Usage\
**Created:** [December 15, 2022, 11:51am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-add-method-to-base-maininclude-eval/91697 "2022-12-15T11:51:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Post date:** [December 15, 2022, 11:51am UTC](https://discourse.julialang.org/t/whats-a-good-way-to-add-method-to-base-maininclude-eval/91697/1 "2022-12-15T11:51:11Z")

</div>

My module provides utilities to generate optimized code based on user input.  
For instance, a `generate` function returns values of type `Expr`, that the user is responsible to [`eval`](https://docs.julialang.org/en/v1/base/base/#Base.MainInclude.eval)uate whenever they need:

```julia
code, data = generate(input)
use_generated_code(eval(code), data)

```

My problem is that the generated code has many `LineNumberNode`s inserted, which makes the generated code difficult to inspect for end users. Still, I’m willing to keep these nodes to ease debugging. So I have chosen to wrap it into some opaque newtype:

```julia
struct GeneratedCode
    code::Expr
end
function Base.show(io::IO, c::GeneratedCode)
    print(io, "GeneratedCode{")
    print(io, Base.remove_linenums!(deepcopy(c.code)))
    print(io, "}")
end
# But I can't get the following overload right:
eval(c::GeneratedCode) = eval(c.code)

```

How to correctly overload `eval` then? I have tried with `Core.eval` and `Base.MainInclude.eval` instead, with no luck, because it seems that a new `eval` function is created within every module, right? Is there no way I can achieve something like the above?

---

<div class="post-metadata">

**Author:** ![sunoru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunoru/32/461_2.png) [@sunoru](https://discourse.julialang.org/u/sunoru)\
**Post date:** [December 15, 2022, 7:34pm UTC](https://discourse.julialang.org/t/whats-a-good-way-to-add-method-to-base-maininclude-eval/91697/2 "2022-12-15T19:34:38Z")

</div>

Add

```julia
Core.eval(m::Module, c::GeneratedCode) = Core.eval(m, c.code)

```

should work. The new `eval` function created within every module is actually just something like `eval(x) = Core.eval(@ __MODULE__ , x)`.
