# How to calculate the typeof an expression without actually running it?

**URL:** https://discourse.julialang.org/t/how-to-calculate-the-typeof-an-expression-without-actually-running-it/7211
**Category:** General Usage
**Created:** [November 21, 2017, 4:42am UTC](https://discourse.julialang.org/t/how-to-calculate-the-typeof-an-expression-without-actually-running-it/7211 "2017-11-21T04:42:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [November 21, 2017, 4:42am UTC](https://discourse.julialang.org/t/how-to-calculate-the-typeof-an-expression-without-actually-running-it/7211/1 "2017-11-21T04:42:24Z")

</div>

Hi,

I would like to calculate the typeof an expression without running it. For example:

```julia
function f(x::Int)
    y = x+3
    if x > 0
        y+=1
    else
       y-=3
    end
    x+y
end

```

for example in a macro i am working with the expressions of this function and is there a way to calculate y+=1 to be type of Int?

Thanks

---

<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: [November 21, 2017, 5:13am UTC](https://discourse.julialang.org/t/how-to-calculate-the-typeof-an-expression-without-actually-running-it/7211/2 "2017-11-21T05:13:30Z")

</div>

You can get some information from inference but it’s in general impossible.

---

<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: [November 21, 2017, 5:14am UTC](https://discourse.julialang.org/t/how-to-calculate-the-typeof-an-expression-without-actually-running-it/7211/3 "2017-11-21T05:14:40Z")

</div>

And calling inference in your macro is almost certainly wrong.

---

<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: [November 21, 2017, 5:09pm UTC](https://discourse.julialang.org/t/how-to-calculate-the-typeof-an-expression-without-actually-running-it/7211/4 "2017-11-21T17:09:39Z")

</div>

If you can elaborate a bit on what you’re trying to do, we may be able to provide some more useful advice. What is your macro doing that would require knowing the type of y inside the macro?

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [November 21, 2017, 7:34pm UTC](https://discourse.julialang.org/t/how-to-calculate-the-typeof-an-expression-without-actually-running-it/7211/5 "2017-11-21T19:34:38Z")

</div>

Like yuyichao said this is probably not a good idea, but just for fun and education, there’s a macro (`@code_typed`) that shows you the inferred types. You can find what function it’s calling by doing:

```
macroexpand(:(@code_typed f(2)))

```

Turns out it’s calling `code_typed` in Base:

```
codeinfo = Base.code_typed(f,Tuple{Int64})[1]

```

You can get the inferred types from the `CodeInfo` object, e.g.:

```julia
function f(x) 
    z = 3
    y = 2.0x
end

codeinfo = Base.code_typed(f,Tuple{Int64})[1]
collect(zip(codeinfo.first.slotnames, codeinfo.first.slottypes))

4-element Array{Tuple{Any,Any},1}:
(Symbol("#self#"), #f)
(:x, Int64)
(:z, Int64)
(:y, Float64)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 21, 2017, 8:42pm UTC](https://discourse.julialang.org/t/how-to-calculate-the-typeof-an-expression-without-actually-running-it/7211/6 "2017-11-21T20:42:46Z")

</div>

`Core.Inference.return_type`. See it in action here:

[https://github.com/ChrisRackauckas/VectorizedRoutines.jl/blob/master/src/julia.jl#L44](https://github.com/ChrisRackauckas/VectorizedRoutines.jl/blob/master/src/julia.jl#L44)
