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

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

defining

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

when I do

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

I get

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 :smiley:

Strangely this instead works:

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

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> 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]
4 Likes

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