# Usage of \`@eval\` in Base code when defining a NamedTuple constructor

**URL:** <https://discourse.julialang.org/t/usage-of-eval-in-base-code-when-defining-a-namedtuple-constructor/45328>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 21, 2020, 4:02pm UTC](https://discourse.julialang.org/t/usage-of-eval-in-base-code-when-defining-a-namedtuple-constructor/45328 "2020-08-21T16:02:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 21, 2020, 4:02pm UTC](https://discourse.julialang.org/t/usage-of-eval-in-base-code-when-defining-a-namedtuple-constructor/45328/1 "2020-08-21T16:02:30Z")

</div>

Can anyone explain to me what is going on in this chunk of code from Base?

```julia
@eval function NamedTuple{names,T}(args::Tuple) where {names, T <: Tuple}
    if length(args) != length(names::Tuple)
        throw(ArgumentError("Wrong number of arguments to named tuple constructor."))
    end
    # Note T(args) might not return something of type T; e.g.
    # Tuple{Type{Float64}}((Float64,)) returns a Tuple{DataType}
    $(Expr(:splatnew, :(NamedTuple{names,T}), :(T(args))))
end

```

It looks sort of like a generated function, but it’s not a generated function. Why is the `@eval` needed for this constructor definition?

Here’s a permalink to the repo:

[https://github.com/JuliaLang/julia/blob/89b018f56090a7371ce3826d8ea7d8aa92961a2e/base/namedtuple.jl#L84](https://github.com/JuliaLang/julia/blob/89b018f56090a7371ce3826d8ea7d8aa92961a2e/base/namedtuple.jl#L84)

---

<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:** [August 21, 2020, 4:14pm UTC](https://discourse.julialang.org/t/usage-of-eval-in-base-code-when-defining-a-namedtuple-constructor/45328/2 "2020-08-21T16:14:34Z")

</div>

It’s not a generated function and is not related. It’s just for emitting expression (splatnew) that doesn’t have a parsable syntax in this context.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 21, 2020, 4:56pm UTC](https://discourse.julialang.org/t/usage-of-eval-in-base-code-when-defining-a-namedtuple-constructor/45328/3 "2020-08-21T16:56:33Z")

</div>

I see that this code was introduced in [this PR](https://github.com/JuliaLang/julia/pull/33303) to fix [this bug](https://github.com/JuliaLang/julia/issues/33270). Before that PR, the constructor had a normal definition without `@eval`.

---

<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:** [August 21, 2020, 5:25pm UTC](https://discourse.julialang.org/t/usage-of-eval-in-base-code-when-defining-a-namedtuple-constructor/45328/4 "2020-08-21T17:25:12Z")

</div>

Eval wasn’t the fix, the direct use of the new expression was. And the use of that expression necessitate the use of eval in this context as mentioned above.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 21, 2020, 6:34pm UTC](https://discourse.julialang.org/t/usage-of-eval-in-base-code-when-defining-a-namedtuple-constructor/45328/5 "2020-08-21T18:34:12Z")

</div>

Sure, I didn’t mean to imply that using `@eval` is what fixed the problem.

To rephrase what you’re saying, there’s no normal syntax to create a `Expr(:splatnew, ...)` expression, hence the usage of `Expr(:splatnew, ...)` plus an `eval`. As for why the `Expr(:splatnew, ...)` is needed here in the first place, I’d have to do a bit more studying to figure that out…

---

<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:** [August 21, 2020, 6:52pm UTC](https://discourse.julialang.org/t/usage-of-eval-in-base-code-when-defining-a-namedtuple-constructor/45328/6 "2020-08-21T18:52:46Z")

</div>

There is syntax for it, inside the struct definition, but not in this context.
