# If and symbol, bug or feature?

**URL:** https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729
**Category:** General Usage
**Tags:** syntax, parser
**Created:** [February 3, 2022, 1:13pm UTC](https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729 "2022-02-03T13:13:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [February 3, 2022, 1:13pm UTC](https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729/1 "2022-02-03T13:13:54Z")

</div>

Hi,

first of all many thanks for Julia … what a great language!

As I could not find this issue, I post it here:  
When using an if statement with a symbol, the parser seems to understand it differently as I would:

```julia
if true :a else :b end

```

results in an UndefVarError: a not defined (tested in Julia 1.6 and 1.7).  
Everything works fine when using Symbol(:a) or return :a instead. @code\_lowered for the above form gives  
CodeInfo(  
1 ─ %1 = true:Main.a  
└── goto #3 if not %1  
2 ─ return nothing  
3 ─ return :b  
)

Not even sure what this would mean? Is that interpretation intended?

---

<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: [February 3, 2022, 1:26pm UTC](https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729/2 "2022-02-03T13:26:22Z")

</div>

> [@bertschi](#):
>
> Is that interpretation intended?

```julia
if true 
    :a
else 
    :b 
end

```

yields

```julia
:a

```

But even the form `if true :a; else :b; end` results in the mentioned error. I’m perplexed.

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [February 3, 2022, 1:28pm UTC](https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729/3 "2022-02-03T13:28:50Z")

</div>

`if true; :a else :b end`

works as “intended”

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 3, 2022, 1:28pm UTC](https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729/4 "2022-02-03T13:28:51Z")

</div>

```julia
julia> if true; :a; else; :b; end
:a

```

I guess it’s just a parsing corner case with what `:` binds to which probably doesn’t come up to often as people tend to not write `if ... else ... end` statements as one liners (as that’s what `true ? :a : :b` is for)

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [February 3, 2022, 1:52pm UTC](https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729/5 "2022-02-03T13:52:50Z")

</div>

To clarify, it is parsed as `if (true:a) else :b end`.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [February 3, 2022, 1:53pm UTC](https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729/6 "2022-02-03T13:53:41Z")

</div>

There seems to be several inconsistencies… These all fail:

```julia
julia> if true :a end
ERROR: UndefVarError: a not defined

julia> if true (:a) end
ERROR: syntax: space before "(" not allowed in "true (" at REPL[29]:1

julia> if true [1] end
ERROR: syntax: space before "[" not allowed in "true [" at REPL[31]:1

julia> if true (1,) end
ERROR: syntax: space before "(" not allowed in "true (" at REPL[32]:1

```

while these work:

```julia
julia> if true Symbol('a') end
:a

julia> if true "a" end
"a"

julia> if true 1 end
1

julia> if true 'a' end
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> if true `a` end
`a`

julia> if true hypot(1,2) end
2.23606797749979

```

Please find an issue…

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [February 3, 2022, 3:12pm UTC](https://discourse.julialang.org/t/if-and-symbol-bug-or-feature/75729/7 "2022-02-03T15:12:41Z")

</div>

Thanks, I have opened an issue.
