# Why can't I get a symbol to interpolate when using ':(for outer $var ...)'

**URL:** https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987
**Category:** General Usage
**Tags:** question
**Created:** [September 3, 2020, 6:08am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987 "2020-09-03T06:08:06Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![frtps](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@frtps](https://discourse.julialang.org/u/frtps)
#### Post date: [September 3, 2020, 6:08am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/1 "2020-09-03T06:08:06Z")

</div>

I am auto-generating Julia code and wish to construct an Expr for a `for` statement for which the iteration variable will be available in the enclosing scope (that’s the spec of the source language). For some reason

```julia
julia > q = :p
julia> :(for $q = 1:2:3 end)
:(for p = 1:2:3
  end)

```

works, but with `outer` instead I get

```julia
julia> :(for outer $q = 1:2:3 end)
:(for outer $ q = 1:2:3
  end)

```

ie `$q` has not been interpolated.  
(This is Julia 1.2)

---

<div class="post-metadata">

### Author: ![phg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phg/32/17184_2.png) [@phg](https://discourse.julialang.org/u/phg)
#### Post date: [September 3, 2020, 6:47am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/2 "2020-09-03T06:47:04Z")

</div>

They’re parsed differently:

```julia
julia> Meta.show_sexpr(:(for outer $q = 1:2:3 end))
(:for, (:(=), (:call, :$, :outer, :q), (:call, :(:), 1, 2, 3)), (:block,
    :(#= REPL[1]:1 =#)
  ))

julia> Meta.show_sexpr(:(for $q = 1:2:3 end))
(:for, (:(=), :p, (:call, :(:), 1, 2, 3)), (:block,
    :(#= REPL[3]:1 =#)
  ))

```

The first case is surprising, even to me, but it must be due to the fact that `outer p` is not valid syntax anyway, so `outer $ p` with `$` interpreted as infix operator is the only interpretation that can work:

```julia
julia> Meta.show_sexpr(:(a$b))
(:call, :$, :a, :b)

```

I had now idea this is possible, TBH.

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [September 3, 2020, 6:55am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/3 "2020-09-03T06:55:46Z")

</div>

The `outer` keyword exists in Julia 1.5 but not in Julia 1.2. So if you want to use it, you need to update your Julia version. To be honest I had not idea this keyword exists but it is in the manual: [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Loops-and-Comprehensions)

---

<div class="post-metadata">

### Author: ![phg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phg/32/17184_2.png) [@phg](https://discourse.julialang.org/u/phg)
#### Post date: [September 3, 2020, 6:58am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/4 "2020-09-03T06:58:41Z")

</div>

> [@josuagrw](#):
>
> The `outer` keyword exists in Julia 1.5 but not in Julia 1.2.

Uh. That’s new to me, never heard of it. My examples were done in 1.3. Time to update!

---

<div class="post-metadata">

### Author: ![frtps](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@frtps](https://discourse.julialang.org/u/frtps)
#### Post date: [September 3, 2020, 7:29am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/5 "2020-09-03T07:29:19Z")

</div>

The manual for 1.2 includes `outer` in the section on [Loop scoping](https://docs.julialang.org/en/v1.2/manual/variables-and-scoping/#For-Loops-and-Comprehensions-1). It is however missing from [the list of keywords](https://docs.julialang.org/en/v1.2/base/base/#Keywords-1) in the manual for both 1.2 and 1.5. Anyway, I’ll give 1.5 a try.

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [September 3, 2020, 7:43am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/6 "2020-09-03T07:43:25Z")

</div>

Good point – maybe the issue is that you are running this code the REPL? Especially around variable scope the REPL has special rules

---

<div class="post-metadata">

### Author: ![frtps](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@frtps](https://discourse.julialang.org/u/frtps)
#### Post date: [September 3, 2020, 8:20am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/7 "2020-09-03T08:20:08Z")

</div>

I initially encountered this problem inside a function so I don’t think REPL scope is the issue.

---

<div class="post-metadata">

### Author: ![frtps](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@frtps](https://discourse.julialang.org/u/frtps)
#### Post date: [September 3, 2020, 8:22am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/8 "2020-09-03T08:22:03Z")

</div>

Just checked with Julia 1.5.1, the behaviour is the same as Julia 1.2.

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [September 3, 2020, 8:23am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/9 "2020-09-03T08:23:40Z")

</div>

So, it seems like the parser does not recognize the `outer` keyword properly. I think that justifies filing an issue.

---

<div class="post-metadata">

### Author: ![frtps](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@frtps](https://discourse.julialang.org/u/frtps)
#### Post date: [September 4, 2020, 1:22am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/10 "2020-09-04T01:22:36Z")

</div>

I’ve [raised an issue](https://github.com/JuliaLang/julia/issues/37393).

---

<div class="post-metadata">

### Author: ![frtps](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@frtps](https://discourse.julialang.org/u/frtps)
#### Post date: [October 19, 2020, 12:51am UTC](https://discourse.julialang.org/t/why-cant-i-get-a-symbol-to-interpolate-when-using-for-outer-var/45987/11 "2020-10-19T00:51:02Z")

</div>

A [fix for this issue](https://github.com/JuliaLang/julia/pull/37402) has now been committed to master.
