# Quoting operator & literals

**URL:** https://discourse.julialang.org/t/quoting-operator-literals/1327
**Category:** General Usage
**Tags:** question
**Created:** [January 6, 2017, 3:30pm UTC](https://discourse.julialang.org/t/quoting-operator-literals/1327 "2017-01-06T15:30:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mike\_Aizatsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_aizatsky/32/433_2.png) [@Mike\_Aizatsky](https://discourse.julialang.org/u/Mike_Aizatsky)
#### Post date: [January 6, 2017, 3:30pm UTC](https://discourse.julialang.org/t/quoting-operator-literals/1327/1 "2017-01-06T15:30:20Z")

</div>

I’m trying to write some meta-code and I found that : operator return type is inconsistent:

```julia
> typeof(:(x)), typeof(:(1))
(Symbol,Int64)

```

I would expect the result of : to be always Expr, but it doesn’t seem to be true for literal expressions.

Is this by design? How do I pass literal expressions to functions that expect Expr?

---

<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: [January 6, 2017, 3:36pm UTC](https://discourse.julialang.org/t/quoting-operator-literals/1327/2 "2017-01-06T15:36:15Z")

</div>

In Julia, `Expr` is a specific type with a `head` and `args` for representing ASTs. But other types can be expressions _per se_ or part of expressions: such as symbols, and other atoms. Your function should handle these too, so it should not dispatch on `Expr` only (unless that’s the intention).

---

<div class="post-metadata">

### Author: ![Mike\_Aizatsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_aizatsky/32/433_2.png) [@Mike\_Aizatsky](https://discourse.julialang.org/u/Mike_Aizatsky)
#### Post date: [January 6, 2017, 3:47pm UTC](https://discourse.julialang.org/t/quoting-operator-literals/1327/3 "2017-01-06T15:47:31Z")

</div>

Tamas,

I understand what you are saying but this decision doesn’t make it a convenient to write meta code.

I have a data type:

```julia
immutable P
  f::Expr
end

```

Where f will be something that will be symbolically differentiatable. (I will use rdiff). How should I change f data type in this case? Does it also mean that everywhere else I have in parameters f::Expr I have to do something else as well? Should I detect literales manually and wrap them somehow into Expr before passing to rdiff?

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [January 6, 2017, 4:01pm UTC](https://discourse.julialang.org/t/quoting-operator-literals/1327/4 "2017-01-06T16:01:47Z")

</div>

My advice: don’t try to force things into a uniform `Expr` form. It makes meta-programming code less suited to Julia’s strengths because you can clean up Julia meta-programming code by dispatching on types rather than writing big case statements.

You can see how you could use dispatch in [https://github.com/johnmyleswhite/Calculus.jl/blob/master/src/symbolic.jl](https://github.com/johnmyleswhite/Calculus.jl/blob/master/src/symbolic.jl). That code could even be more aggressive by adding the head symbols to the types of individual `Expr` objects to distinguish them using types alone.

---

<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: [January 6, 2017, 4:14pm UTC](https://discourse.julialang.org/t/quoting-operator-literals/1327/5 "2017-01-06T16:14:10Z")

</div>

Anything can be an expression (or part of an expression) in Julia. Eg

```julia
julia> immutable Foo x end
julia> dump(:(x + $(Foo(9))))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol +
    2: Symbol x
    3: Foo
      x: Int64 9
  typ: Any

```

so `Foo(9)` is just sitting there as an argument until `+` is called, and it is not an `Expr`, which `:(Foo(9))` would be. (See [this comment](https://discourse.julialang.org/t/how-to-construct-expr-tuple-using-templates/1305/2) on a question I asked recently for related concepts).

More related to your application: I imagine a symbol eg `:x` is also symbolically differentiable, and it is not an expression. So don’t restrict the types.

Finally, you may find this library useful:

> **[GitHub - FluxML/MacroTools.jl: MacroTools provides a library of tools for...](https://github.com/FluxML/MacroTools.jl)**
>
> MacroTools provides a library of tools for working with Julia code and expressions. - GitHub - FluxML/MacroTools.jl: MacroTools provides a library of tools for working with Julia code and expressions.
