# How to detect variables of type \`Any\` at compiletime?

**URL:** https://discourse.julialang.org/t/how-to-detect-variables-of-type-any-at-compiletime/4568
**Category:** General Usage
**Tags:** macros, inference, type
**Created:** [June 30, 2017, 3:18pm UTC](https://discourse.julialang.org/t/how-to-detect-variables-of-type-any-at-compiletime/4568 "2017-06-30T15:18:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)
#### Post date: [June 30, 2017, 3:18pm UTC](https://discourse.julialang.org/t/how-to-detect-variables-of-type-any-at-compiletime/4568/1 "2017-06-30T15:18:30Z")

</div>

I want a macro that detects (and makes the function fail or warns me) if some variable is not inferred correctly, if the variable’s type is inferred, then it should do nothing.  
E.g. something like this:

```julia
j = 3
function f(x)
    for i in 1:100 
        @warn_any j
        x += j
    end
    return x
end

```

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [June 30, 2017, 7:00pm UTC](https://discourse.julialang.org/t/how-to-detect-variables-of-type-any-at-compiletime/4568/2 "2017-06-30T19:00:59Z")

</div>

can you use `@inferred` to do what you want? That is,

```julia
using Base.Test
function bad(x)
    innerbad(x) = if rand() < 0.5 x else true end
    @inferred innerbad(x)
end

julia> bad(4)
ERROR: return type Int64 does not match inferred return type Union{Bool, Int64}

```

---

<div class="post-metadata">

### Author: ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)
#### Post date: [June 30, 2017, 7:37pm UTC](https://discourse.julialang.org/t/how-to-detect-variables-of-type-any-at-compiletime/4568/3 "2017-06-30T19:37:47Z")

</div>

I did not understand why should @inferred fail here, while

> julia\> Int64 \<: Union{Bool, Int64}  
> true

is true. ???

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [June 30, 2017, 8:16pm UTC](https://discourse.julialang.org/t/how-to-detect-variables-of-type-any-at-compiletime/4568/4 "2017-06-30T20:16:27Z")

</div>

> <https://github.com/JuliaLang/julia/issues/10980>
>
> Given the importance of type stability, I would like to propose a new language c…onstruct that would allow semi-static checking of type stable blocks. In the module I am developing, I am frequently using this type of construct
> 
> \`\`\` julia
> function f(a)
> # Type stability
> @stabilize m::Matrix{Float64} = a.m
> 
> # Compute things with m
> 
> end
> ...
> \`\`\`
> 
> I know there are:
> 1. The possibility to use a function - but this can be cumbersome when there are many types, and in makes the code harder to read (at least for me)
> 2. Tools like Lint or TypeCheck, and the macro \`@code\_warntype\` that are useful to see/check whether the written code is type stable
> 
> but I would really like to have something that prevents code from being instable, e.g. using a "stable block" (I check and this gives a syntax error with julia 0.\[34\]):
> 
> \`\`\` julia
> function f(a)
> # Type stability
> @stabilize m::Matrix{Float64} = a.m
> 
> stable begin
> # Compute things - compilation error (or warning) if there is some type instability within the code
> end
> 
> end
> ...
> \`\`\`
> 
> Another syntactic possibility would be to use pragmas #7449 (if they make it in the language).

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [June 30, 2017, 8:21pm UTC](https://discourse.julialang.org/t/how-to-detect-variables-of-type-any-at-compiletime/4568/5 "2017-06-30T20:21:13Z")

</div>

`@inferred` tests for equality (as per docs).

---

<div class="post-metadata">

### Author: ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)
#### Post date: [July 1, 2017, 1:07pm UTC](https://discourse.julialang.org/t/how-to-detect-variables-of-type-any-at-compiletime/4568/6 "2017-07-01T13:07:14Z")

</div>

Thanks for the tip with `@inferred`, your code still does the checking at runtime and not compiletime. There is no easy way to give just a warning, but this one quite close to what I want:

```julia
macro typestable(x)
    Base.Test.@inferred eval(:(()->$x))()
    return :()
end

const k = 3
l = 3

function f1(x)
    @typestable(k)
    for i in 1:10
        x += k - 1
    end
    return x
end

function f2(x)
    @typestable(l)
    for i in 1:10
        x = x + l
    end
    return x
end

```

Throws an error when sourcing `f2` and gives very nice LLVM assembly for `f1`:

```julia
julia> function f2(x)
+ @typestable(l)
+ for i in 1:10
+ x = x + l
+ end
+ return x
end
ERROR: return type Int64 does not match inferred return type Any

julia> @code_llvm f1(19)

define i64 @julia_f1_71937(i64) #0 {
top:
  %1 = add i64 %0, 20
  ret i64 %1
}

```
