# Iterate on expression

**URL:** <https://discourse.julialang.org/t/iterate-on-expression/21083>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [February 22, 2019, 3:50pm UTC](https://discourse.julialang.org/t/iterate-on-expression/21083 "2019-02-22T15:50:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [February 22, 2019, 3:50pm UTC](https://discourse.julialang.org/t/iterate-on-expression/21083/1 "2019-02-22T15:50:16Z")

</div>

Why would one ever want to iterate over expressions? Well its like that: I have a recursive function on expr, to replace a Symbol with a given expression.

```julia
ersatz(ex::Symbol,a::Symbol,b) = ex==a ? deepcopy(b) : ex
ersatz(ex::Expr ,a::Symbol,b) = Expr(ex.head,ersatz.(ex.args,a,b)...)  

show(ersatz(:(12+a*c^3),:a,:(2+2)))

```

You notice line 2, I can’t be bothered with a for loop, so I use the dot syntax for functions. Now the dot syntax must examine the size and iterative over, all its argument. The last argument, b, tends to be an Expression… for which neither length nor iterate are defined. Boom (Julia 1.0).

My claim: to make dot syntax universally applicable, any type in Julia must be iterable - even though that may be just one step.

And this is not a request for assistance, thank you, I will cope with a for loop! 😀

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 22, 2019, 3:53pm UTC](https://discourse.julialang.org/t/iterate-on-expression/21083/2 "2019-02-22T15:53:32Z")

</div>

> [@Philippe\_Maincon1](#):
>
> Boom

`Ref` or [friends](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting-1)

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [February 22, 2019, 4:02pm UTC](https://discourse.julialang.org/t/iterate-on-expression/21083/3 "2019-02-22T16:02:23Z")

</div>

Ref(b) is iterable, and it works! Thank you!
