# Code generation: unnecessary comment lines when using "quote"

**URL:** https://discourse.julialang.org/t/code-generation-unnecessary-comment-lines-when-using-quote/398
**Category:** General Usage
**Created:** [November 17, 2016, 10:33pm UTC](https://discourse.julialang.org/t/code-generation-unnecessary-comment-lines-when-using-quote/398 "2016-11-17T22:33:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)
#### Post date: [November 17, 2016, 10:33pm UTC](https://discourse.julialang.org/t/code-generation-unnecessary-comment-lines-when-using-quote/398/1 "2016-11-17T22:33:09Z")

</div>

Trying to generate a type in 2 ways:

```julia
Expr(:type, false, :Oo, Expr(:block, :(field::Int)))

```

and

```julia
quote
  immutable Oo
    field::Int
  end
end

```

I’ve got 2 functionally equivalent, but different results for `Expr` and `quote` approaches respectively:

```julia
immutable Oo
  field::Int
end

```

and

```julia
begin # console, line 2
  immutable Oo # console, line 3
    field::Int
  end
end

```

The `quote` approach for some reason generates additional `line` comments, and wraps the type in `begin ... end` block.  
**Question** : is it possible to get rid of this redundant info?

---

<div class="post-metadata">

### Author: ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)
#### Post date: [November 17, 2016, 10:53pm UTC](https://discourse.julialang.org/t/code-generation-unnecessary-comment-lines-when-using-quote/398/2 "2016-11-17T22:53:33Z")

</div>

What version of Julia are you using?

To get rid of the wrapping, try:

```julia
:(
  immutable Oo
    field::Int
  end
)

```

---

<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: [November 17, 2016, 11:17pm UTC](https://discourse.julialang.org/t/code-generation-unnecessary-comment-lines-when-using-quote/398/3 "2016-11-17T23:17:06Z")

</div>

Those are not comments, but line number info necessary to generate the debug info. You can just ignore them when you are processing the AST. If you are writing a macro or otherwise transforming the AST before evaluting it or passing it to the compiler, you should keep the line number nodes whenever possible. If you remove them, it’ll be impossible to generate correct line numbers in the code you generate.

```nohighlight
julia> function filter_lineno(ex::Expr)
           filter!(ex.args) do e
               isa(e, LineNumberNode) && return false
               if isa(e, Expr)
                   (e::Expr).head === :line && return false
                   filter_lineno(e::Expr)
               end
               return true
           end
           return ex
       end
filter_lineno (generic function with 1 method)

julia> macro m1(ex::Expr)
           esc(filter_lineno(ex))
       end
@m1 (macro with 1 method)

julia> macro m2(ex::Expr)
           esc(ex)
       end
@m2 (macro with 1 method)

julia> @m1 function f()
           error()
       end
f (generic function with 1 method)

julia> @m2 function f2()
           error()
       end
f2 (generic function with 1 method)

julia> f()
ERROR: 
 in f() at ./<missing>:0

julia> f2()
ERROR: 
 in f2() at ./REPL[5]:2

```

---

<div class="post-metadata">

### Author: ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)
#### Post date: [November 18, 2016, 3:21pm UTC](https://discourse.julialang.org/t/code-generation-unnecessary-comment-lines-when-using-quote/398/4 "2016-11-18T15:21:00Z")

</div>

Ah, actually this is that _unlikely_ case, when code needs to be generated and saved to a file (I need to follow up some generated structures).  
I thought that there might be some flag, which disables this behavior, but **filter\_lineno** solves the problem, thanks! Btw, why do you use **esc()**, isn’t it redundant?

---

<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: [November 19, 2016, 11:34pm UTC](https://discourse.julialang.org/t/code-generation-unnecessary-comment-lines-when-using-quote/398/5 "2016-11-19T23:34:04Z")

</div>

> [@wrgr](#):
>
> Ah, actually this is that unlikely case, when code needs to be generated and saved to a file (I need to follow up some generated structures).

It might still be useful to keep those node in case you ever need to map the generated file back to the source. In any case, the list above should give you the kind of nodes to ignore.

> [@wrgr](#):
>
> Btw, why do you use esc(), isn’t it redundant?

It is necessary. I general, you must use `esc` in a macro on user code that you treat as black box (or anything you generate that should reference caller context in general). Of course you don’t need it if you are generating a file.
