# Issue with macros and \`x\[end\]\`

**URL:** <https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850>\
**Category:** Internals & Design\
**Created:** [September 12, 2018, 11:01am UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850 "2018-09-12T11:01:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [September 12, 2018, 11:01am UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/1 "2018-09-12T11:01:11Z")

</div>

Macros understand `getindex` syntax for numbers but not `end` word and throw error.

```julia
julia> @which xdots[end]
ERROR: UndefVarError: end not defined
Stacktrace:
 [1] top-level scope at none:0

julia> @which xdots[1]
getindex(A::Array, i1::Int64) in Base at array.jl:731

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 12, 2018, 11:27am UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/2 "2018-09-12T11:27:00Z")

</div>

No, this is an issue of the `@which` macro:

```julia
julia> macro m(x)
       @show x
       x
       end                                                                                                                                                              
@m (macro with 1 method)                                                                                                                                                

julia> x = [1]                                                                                                                                                          
1-element Array{Int64,1}:                                                                                                                                               
 1                                                                                                                                                                      

julia> @m x[end]                                                                                                                                                        
x = :(x[end])                                                                                                                                                           
1                                                                                                                                                                       

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [September 12, 2018, 11:30am UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/3 "2018-09-12T11:30:46Z")

</div>

However many other macros (`@code_*, @less, @edit`) in Base have the same issue.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 12, 2018, 12:08pm UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/4 "2018-09-12T12:08:32Z")

</div>

Yes, looking at the macroexpand:

```julia
julia> @macroexpand @which x[end]
:((InteractiveUtils.which)(getindex, (Base.typesof)(x, end)))

julia> @macroexpand @which x[1]
:((InteractiveUtils.which)(getindex, (Base.typesof)(x, 1)))

```

so, what should be done is check for an `end` and if found instead expand to

```julia
InteractiveUtils.which(getindex, (Base.typesof)(x, lastindex(x))) 

```

However, that might be more trouble than it is worth.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 12, 2018, 1:07pm UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/5 "2018-09-12T13:07:47Z")

</div>

Lowering code already does this though, so the right way to fix this may be to do lowering first and then do the method lookup on whatever it gets lowered to. Could someone file an issue about this bug?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 12, 2018, 2:58pm UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/6 "2018-09-12T14:58:58Z")

</div>

> [@StefanKarpinski](#):
>
> the right way to fix this may be to do lowering first and then do the method lookup on whatever it gets lowered to

Yeah, that’s how it used to work, and in this case it might work, but lowering does _lots_ of other things and often will expand out to more than one line of code. So we’ve settled on just implementing a small subset of the “helpful” lowering manually within the macro itself. I believe we do have Julia code somewhere in Base that does this part of the lowering transform, so we could probably easily perform it here, too.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [September 14, 2018, 7:08am UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/7 "2018-09-14T07:08:59Z")

</div>

I submitted an issue at [Issue with edit, which, etc. macros for `x[end]` · Issue #29177 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/29177) .

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 14, 2018, 7:26am UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/8 "2018-09-14T07:26:59Z")

</div>

Why don’t you add your first example to the issue, saves people some clicks.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [September 14, 2018, 7:40am UTC](https://discourse.julialang.org/t/issue-with-macros-and-x-end/14850/9 "2018-09-14T07:40:29Z")

</div>

My bad. I updated with examples and your comment.
