# How to check the inferred type of a specific variable in a function?

**URL:** <https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962>\
**Category:** Internals & Design\
**Created:** [December 24, 2018, 2:34pm UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962 "2018-12-24T14:34:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 24, 2018, 2:34pm UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/1 "2018-12-24T14:34:56Z")

</div>

Hi,

I am having this problem which I can solve best if I could find out the inferred type of certain variables in a function. Basically, I am looking for a generalized `Core.Compiler.return_type` for arbitrary variables, not just the returned ones. Related thread: [Ridiculous idea: types from the future](https://discourse.julialang.org/t/ridiculous-idea-types-from-the-future/5457). Unfortunately, in my case it is not a simple map so I cannot use the solution from there. The array buffer would work for me, but is less than ideal. I thought of manipulating the function with a macro (that’s an option) to return the variables I am interested in, then use `Core.Compiler.return_type` but then I have to worry about multiple return points in the original function, which is not robust I feel due to `throw`s, macros, etc which may be in the function body. Any suggestions? A toy MWE is simple:

```julia
function f(x)
    a = x * 2
    y = a / 3 # what is the inferred type of y?
    return 
end

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 24, 2018, 2:51pm UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/2 "2018-12-24T14:51:28Z")

</div>

`@code_warntype` prints the info in the repl and `typeof(y) ` gives you the type of y at runtime.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 24, 2018, 2:59pm UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/3 "2018-12-24T14:59:39Z")

</div>

What I am after is something like the output of `@code_warntype` programmatically at compile time.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 24, 2018, 3:21pm UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/4 "2018-12-24T15:21:30Z")

</div>

Perhaps looking at the implementation of code\_wantype can hint at what functionality is used there?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 24, 2018, 11:36pm UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/5 "2018-12-24T23:36:35Z")

</div>

> Perhaps looking at the implementation of code\_wantype can hint at what functionality is used there?

It seems pretty cryptic.

---

<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:** [December 25, 2018, 7:44am UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/6 "2018-12-25T07:44:51Z")

</div>

I would either use a function barrier, or `typeof` as @baggepinnen suggested above. If the type can be inferred, this is costless, if not, then you have a fallback.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 25, 2018, 9:38am UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/7 "2018-12-25T09:38:28Z")

</div>

Oh well, perhaps moving it to internals will get more views from core devs before I give up on this. I will use the function barrier with the buffer if nothing else works.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 25, 2018, 10:23am UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/8 "2018-12-25T10:23:27Z")

</div>

What are you trying to do with the information?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 25, 2018, 11:04am UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/9 "2018-12-25T11:04:18Z")

</div>

I want to make a parameterized struct with a type parameter as the type of `y` before `y` is defined. But it is not just `y`, it can also be `a` and other variables if I want, some of these may be arrays. The use case is a bit involved so I am not sure more information will be too helpful here. If what I am asking is not possible, I will assume the parameter is `Any` at first, then narrow it down later.

---

<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:** [December 25, 2018, 11:59am UTC](https://discourse.julialang.org/t/how-to-check-the-inferred-type-of-a-specific-variable-in-a-function/18962/10 "2018-12-25T11:59:53Z")

</div>

> [@mohamed82008](#):
>
> The use case is a bit involved so I am not sure more information will be too helpful here.

I believe that an example that demonstrates more context would be very useful. The specific solution you are after may not be the idiomatic one, there may be another way.
