# Literals as expressions

**URL:** https://discourse.julialang.org/t/literals-as-expressions/47415
**Category:** General Usage
**Created:** [September 28, 2020, 4:05pm UTC](https://discourse.julialang.org/t/literals-as-expressions/47415 "2020-09-28T16:05:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mikepol](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mikepol](https://discourse.julialang.org/u/mikepol)
#### Post date: [September 28, 2020, 4:05pm UTC](https://discourse.julialang.org/t/literals-as-expressions/47415/1 "2020-09-28T16:05:09Z")

</div>

Hi,

I’m trying to represent a literal as an Expr object, without success:

```julia
julia> :(5) |> typeof
Int64

julia> Meta.parse("5") |> typeof
Int64

julia> function foo(e::Expr) e end
julia> foo(:(5))
ERROR: MethodError: no method matching foo(::Int64)

```

What I’m trying to do is to store expression objects and later combine them, but I’m unable to store literals. Something along the lines of:

```julia
struct Foo
  expr::Expr
end

function combine(op::Symbol, x::Foo, y::Foo)
  return Foo(:($op($(x.expr),$(y.expr))))
end

```

```julia
julia> combine(:+,Foo(:(5*x)),Foo(:(y-1)))
Foo(:(5x + (y - 1)))

julia> combine(:+,Foo(:(5)),Foo(:(y-1)))
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Expr

```

Is it possible to store literals as Expr objects, or should I hold my expressions as type Any?

Thanks.  
-Mike

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [September 28, 2020, 4:14pm UTC](https://discourse.julialang.org/t/literals-as-expressions/47415/2 "2020-09-28T16:14:40Z")

</div>

`::Any` is probably fine here. Alternatively, you could declare a more specific type alias like:

```julia
const ArgType = Union{Expr, Symbol, String, Number}

```

This will likely have little or no performance benefit over using `::Any`, but you might find it helpful in terms of documenting and checking your expected types.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 28, 2020, 4:15pm UTC](https://discourse.julialang.org/t/literals-as-expressions/47415/3 "2020-09-28T16:15:41Z")

</div>

Literals are literal, so you don’t need to convert them to symbols. They are stored as themselves in `Expr` objects:

```julia
julia> dump(:(1 + 2))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol +
    2: Int64 1
    3: Int64 2

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [September 28, 2020, 4:28pm UTC](https://discourse.julialang.org/t/literals-as-expressions/47415/4 "2020-09-28T16:28:39Z")

</div>

No you should use `Any` if you want to handle anything that can be returned by the parser/lowering. `Expr` is for, well, expression, and there are syntax that aren’t really expressions, for example literals…

---

<div class="post-metadata">

### Author: ![mikepol](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mikepol](https://discourse.julialang.org/u/mikepol)
#### Post date: [September 28, 2020, 4:36pm UTC](https://discourse.julialang.org/t/literals-as-expressions/47415/5 "2020-09-28T16:36:25Z")

</div>

Ok I think I understand now. I can use Union if I know before hand what types of literals I’ll be storing, but if I want to store literals of any type down the road, I really need to use Any. So I think Any is appropriate here. Thanks everyone for your help!
