# Problem with escaping anonymous function expressions and let blocks

**URL:** <https://discourse.julialang.org/t/problem-with-escaping-anonymous-function-expressions-and-let-blocks/1314>\
**Category:** General Usage\
**Created:** [January 5, 2017, 8:47pm UTC](https://discourse.julialang.org/t/problem-with-escaping-anonymous-function-expressions-and-let-blocks/1314 "2017-01-05T20:47:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ti-s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti-s/32/25619_2.png) [@ti-s](https://discourse.julialang.org/u/ti-s)\
**Post date:** [January 5, 2017, 8:47pm UTC](https://discourse.julialang.org/t/problem-with-escaping-anonymous-function-expressions-and-let-blocks/1314/1 "2017-01-05T20:47:26Z")

</div>

Consider a macro like:

```julia
manipulate_expr(expr) = esc(expr)
macro test(expr)
    Expr(expr.head, esc(expr.args[1]), manipulate_expr.(expr.args[2:end])...)
end

```

`manipulate_expr` is a placeholder for a transformation of the AST. This definition works for most types of expressions I tried, e.g.:

```julia
julia> @macroexpand @test x = 1
:(x = 1)

```

However, I can’t find a way to manipulate the right hand side of an anonymous function:

```julia
julia> @macroexpand @test x -> x
:($(Expr(:error, "\"(escape x)\" is not a valid function argument name")))

```

A similar problem occurs for `let` blocks:

```julia
julia> @test let x = 1; x end
ERROR: syntax: invalid let syntax

```

How can I apply an AST transformation to these expressions?  
(For an example use case, see, e.g., [https://github.com/ti-s/ShowEach.jl](https://github.com/ti-s/ShowEach.jl))

---

<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:** [January 5, 2017, 11:31pm UTC](https://discourse.julialang.org/t/problem-with-escaping-anonymous-function-expressions-and-let-blocks/1314/2 "2017-01-05T23:31:12Z")

</div>

[It’s this issue](https://github.com/JuliaLang/julia/issues/16096#issuecomment-216626004). The only work-around I know is to `esc` the whole expression (i.e. `esc(Expr(...))`)

---

<div class="post-metadata">

**Author:** ![ti-s](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti-s/32/25619_2.png) [@ti-s](https://discourse.julialang.org/u/ti-s)\
**Post date:** [January 6, 2017, 12:09am UTC](https://discourse.julialang.org/t/problem-with-escaping-anonymous-function-expressions-and-let-blocks/1314/3 "2017-01-06T00:09:36Z")

</div>

Ok, then I will use obscure names for the functions that are inserted to make escaping the whole expressions safe.

---

<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:** [January 6, 2017, 1:11am UTC](https://discourse.julialang.org/t/problem-with-escaping-anonymous-function-expressions-and-let-blocks/1314/4 "2017-01-06T01:11:02Z")

</div>

Or you can use `gensym`…
