# Broadcasting in macros

**URL:** <https://discourse.julialang.org/t/broadcasting-in-macros/25797>\
**Category:** General Usage\
**Created:** [June 28, 2019, 12:11pm UTC](https://discourse.julialang.org/t/broadcasting-in-macros/25797 "2019-06-28T12:11:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [June 28, 2019, 12:11pm UTC](https://discourse.julialang.org/t/broadcasting-in-macros/25797/1 "2019-06-28T12:11:36Z")

</div>

What is wrong with this macro:

```julia
julia> macro lessthanthree(vec)
       return :($vec .< 3)
       end
@lessthanthree (macro with 1 method)

julia> @lessthanthree 1:6
ERROR: UndefVarError: .< not defined
Stacktrace:
 [1] top-level scope at none:0

```

The following works:

```julia
julia> vec = 1:6
1:6

julia> eval(:($vec .< 3))
6-element BitArray{1}:
  true
  true
 false
 false
 false
 false

```

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [June 28, 2019, 1:28pm UTC](https://discourse.julialang.org/t/broadcasting-in-macros/25797/2 "2019-06-28T13:28:49Z")

</div>

try returning `esc(:($vec .< 3))`

Julia macros are “hygenic” and can’t see values from the scope where they are invocked unless you use `esc`.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [June 28, 2019, 1:47pm UTC](https://discourse.julialang.org/t/broadcasting-in-macros/25797/3 "2019-06-28T13:47:47Z")

</div>

This looks like a bug to me. It doesn’t happen on 1.2-rc1, but does on 1.1, although `dump(@macroexpand @lessthanthree 1:6)` looks identical. As ninjaaron says, an overall `esc` does seem to avoid it.

This is an error on both 1.1 and 1.2:

```julia
julia> @code_lowered 1:6 .< 3
ERROR: UndefVarError: .< not defined

```

On 0.6.4 you get this warning, and similar on 0.7:

```julia
julia> @lessthanthree 1:6
WARNING: .< is no longer a function object; use broadcast(<, ...) instead

```

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [June 28, 2019, 8:09pm UTC](https://discourse.julialang.org/t/broadcasting-in-macros/25797/4 "2019-06-28T20:09:37Z")

</div>

Thanks, using esc made the macro work for me as well and so does returning ` :(broadcast(<, $vec, 3))`.

I have read the section about hygiene in the manual and I read it again and I still think I only half get it. Just for my own understanding, assuming it is not a bug, what is it about lessthanthree which might be non-hygienic? Overload of . or \<?

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [June 29, 2019, 4:37pm UTC](https://discourse.julialang.org/t/broadcasting-in-macros/25797/5 "2019-06-29T16:37:31Z")

</div>

Nah, it’s probably a bug. .\< Should be accessible in the macro.

On the other hand, that `$vec` should be escaped anyway, because otherwise it’s not going to work if you try to pass the macro a variable in another module.
