# Why a vector with Expr and Symbols inside fails with ifelse?

**URL:** <https://discourse.julialang.org/t/why-a-vector-with-expr-and-symbols-inside-fails-with-ifelse/101665>\
**Category:** General Usage\
**Tags:** question, vector, functions\
**Created:** [July 15, 2023, 11:31pm UTC](https://discourse.julialang.org/t/why-a-vector-with-expr-and-symbols-inside-fails-with-ifelse/101665 "2023-07-15T23:31:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [July 15, 2023, 11:31pm UTC](https://discourse.julialang.org/t/why-a-vector-with-expr-and-symbols-inside-fails-with-ifelse/101665/1 "2023-07-15T23:31:58Z")

</div>

This is probably a stupid question but I don’t understand why this doesn’t work, I’m using Julia 1.9.2:

defining

```julia
a = [:f1, :(f2::Int)]

```

when I do

```julia
[ifelse(typeof(x) == Expr, x.args, x) for x in a]

```

I get

```julia
ERROR: type Symbol has no field args
Stacktrace:
 [1] getproperty
   @ .\Base.jl:37 [inlined]
 [2] (::var"#55#56")(x::Symbol)
   @ Main .\none:0
 [3] iterate
   @ .\generator.jl:47 [inlined]
 [4] collect(itr::Base.Generator{Vector{Any}, var"#55#56"})
   @ Base .\array.jl:782
 [5] top-level scope
   @ REPL[38]:1

```

why the error refers to a symbol when the if is on the expression? And why it fails? Is this a bug? As a last resource, I also tried to switch `x` and `x.args` but nothing changed 😃

Strangely this instead works:

```julia
julia> [ifelse(typeof(x) == Expr, 1, 0) for x in a]
2-element Vector{Int64}:
 0
 1

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 15, 2023, 11:48pm UTC](https://discourse.julialang.org/t/why-a-vector-with-expr-and-symbols-inside-fails-with-ifelse/101665/2 "2023-07-15T23:48:16Z")

</div>

`ifelse` is just a normal function with an `if ... else ... ` structure inside it. As any other function, it does evaluate **all** of its arguments before being called. What you probably wanted to use is the ternary operator `?:`.

```julia
julia> a = [:f1, :(f2::Int)]
2-element Vector{Any}:
 :f1
 :(f2::Int)

julia> [x isa Expr ? x.args : x for x in a]
2-element Vector{Any}:
 :f1
 Any[:f2, :Int]

```

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [July 15, 2023, 11:49pm UTC](https://discourse.julialang.org/t/why-a-vector-with-expr-and-symbols-inside-fails-with-ifelse/101665/3 "2023-07-15T23:49:46Z")

</div>

thank you, my bad, don’t know why I didn’t notice that 😅
