# Function definition Expr that prints incorrectly but evals correctly

**URL:** <https://discourse.julialang.org/t/function-definition-expr-that-prints-incorrectly-but-evals-correctly/128038>\
**Category:** Internals & Design\
**Created:** [April 14, 2025, 2:12am UTC](https://discourse.julialang.org/t/function-definition-expr-that-prints-incorrectly-but-evals-correctly/128038 "2025-04-14T02:12:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 14, 2025, 2:12am UTC](https://discourse.julialang.org/t/function-definition-expr-that-prints-incorrectly-but-evals-correctly/128038/1 "2025-04-14T02:12:47Z")

</div>

@MilesCranmer and I have a discussion over whether `MacroTools.longdef` is correct or not. For reference, `longdef` is meant to take a short-form function definition and turn it into long-form

```julia
julia> MacroTools.longdef(:(f(x) = 10))
:(function f(x)
      #= REPL[50]:1 =#
      10
  end)

```

But consider:

```julia
julia> long_form = longdef(:((a::Int; b=2) -> a + b))
:(function a::Int, #= REPL[44]:1 =#, b = 2
      #= REPL[44]:1 =#
      a + b
  end)

julia> eval(long_form)(1; b=28) # long_form looks weird, but works out OK!
29

julia> dump(long_form)
Expr
  head: Symbol function
  args: Array{Any}((2,))
    1: Expr
      head: Symbol block
      args: Array{Any}((3,))
        1: Expr
          head: Symbol ::
          args: Array{Any}((2,))
            1: Symbol a
            2: Symbol Int
        2: LineNumberNode
          line: Int64 1
          file: Symbol REPL[44]
        3: Expr
          head: Symbol =
          args: Array{Any}((2,))
            1: Symbol b
            2: Int64 2
    2: Expr
      head: Symbol block
      args: Array{Any}((2,))
        1: LineNumberNode
          line: Int64 1
          file: Symbol REPL[44]
        2: Expr
          head: Symbol call
          args: Array{Any}((3,))
            1: Symbol +
            2: Symbol a
            3: Symbol b

```

`long_form` is correct as far as `eval` is concerned, but its printed representation is off. Should we take it as

- “`long_form` is purposefully accepted by `eval`, pretty-printing is just broken for this particular `Expr`”
- OR: “`long_form` isn’t meant to be valid julia code, `eval` accepts it accidentally.”

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 14, 2025, 6:29am UTC](https://discourse.julialang.org/t/function-definition-expr-that-prints-incorrectly-but-evals-correctly/128038/2 "2025-04-14T06:29:29Z")

</div>

The pretty printing seems broken to me, there is no reason it should insert a `,` after the inline comment block/`LineNumberNode`, thereby treating a comment as an argument. My guess is that the printing is a bit too naive here and doesn’t check whether the element in the list is actually an argument expression or not.

Of course, you could also just as well remove the `LineNumberNode` in your expansion.
