# UndefVarError from macro

**URL:** <https://discourse.julialang.org/t/undefvarerror-from-macro/71630>\
**Category:** General Usage\
**Created:** [November 17, 2021, 3:58am UTC](https://discourse.julialang.org/t/undefvarerror-from-macro/71630 "2021-11-17T03:58:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [November 17, 2021, 3:58am UTC](https://discourse.julialang.org/t/undefvarerror-from-macro/71630/1 "2021-11-17T03:58:33Z")

</div>

How can I fix this error?

```julia
using MacroTools: postwalk

macro mymac(e)
    postwalk(x -> (x isa Expr && x.head isa Symbol && x.head == :. 
    ? esc(:($x isa String ? (return $x) : $x isa Int ? $(x).val : throw("invalid"))) : x), e)
end

@mymac function foo(v)
    y = identity(v).ATTR
    return y + 1
end
foo(3) # ERROR: LoadError: UndefVarError: v not defined

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 17, 2021, 12:53pm UTC](https://discourse.julialang.org/t/undefvarerror-from-macro/71630/2 "2021-11-17T12:53:32Z")

</div>

```julia
@macroexpand(@mymac function foo(v)
    y = identity(v).ATTR
    return y + 1
end)

```

shows

```julia
:(function Main.foo(var"#133#v")
      #= C:\Users\Win10\source\repos\julia\idioms\test\problem53.jl:8 =#
      #= C:\Users\Win10\source\repos\julia\idioms\test\problem53.jl:9 =#
      var"#132#y" = if (identity(v)).ATTR isa String
              return (identity(v)).ATTR
          else
              if (identity(v)).ATTR isa Int
                  (identity(v)).ATTR.val
              else
                  throw("invalid")
              end
          end
      #= C:\Users\Win10\source\repos\julia\idioms\test\problem53.jl:10 =#
      return var"#132#y" + 1
  end)

```

It looks to me like you have to adapt the escaping in your macro?
