# Feature request: type assertion for compiler-known type

**URL:** <https://discourse.julialang.org/t/feature-request-type-assertion-for-compiler-known-type/10305>\
**Category:** General Usage\
**Created:** [April 12, 2018, 3:29pm UTC](https://discourse.julialang.org/t/feature-request-type-assertion-for-compiler-known-type/10305 "2018-04-12T15:29:15Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 13, 2018, 7:16am UTC](https://discourse.julialang.org/t/feature-request-type-assertion-for-compiler-known-type/10305/22 "2018-04-13T07:16:32Z")

</div>

> [@Stephen\_Vavasis](#):
>
> My benchmark indicates that even if the inferred type is concrete, the overhead for using the @inferred macro is huge, which I don’t quite understand.

The benchmark expands to:

```julia
julia> @macroexpand @inferred 1+1
quote 
    let 
        begin 
            #1#args = (1, 1)
            #2#result = +(#1#args...)
            #3#inftypes = Base.Test.Base.return_types(+, Base.Test.Base.typesof(#1#args...))
        end
        if (Base.Test.length)(#3#inftypes) == 1
            nothing
        else 
            (Base.throw)(Base.Main.Base.AssertionError("(\$(Expr(:globalref, Base.Test, :(==))))((\$(Expr(:globalref, Base.Test, :length)))(#3#inftypes), 1)"))
        end
        #4#rettype = if #2#result isa Base.Test.Type
                (Base.Test.Type){#2#result}
            else 
                (Base.Test.typeof)(#2#result)
            end
        #4#rettype == #3#inftypes[1] || (Base.Test.error)("return type $(#4#rettype) does not match inferred return type $(#3#inftypes[1])")
        #2#result
    end
end

```

So it will do a bunch of splatting, calling `Base.return_types` etc every time. `@inferred` is written to be used in unit testing (which is why it is in the `Test` module).

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [April 13, 2018, 1:17pm UTC](https://discourse.julialang.org/t/feature-request-type-assertion-for-compiler-known-type/10305/23 "2018-04-13T13:17:57Z")

</div>

Thanks for the example of how `@inferred` expands. It appears from the macro expansion that `@inferred` carries out its test at run-time (“#2#result=+(#1#args…)”) rather than compile time, which raises a question:

The documentation states that it “Tests that the call expression `f(x)` returns a value of the same type inferred by the compiler.” Is this mathematically equivalent to saying that it tests whether the compiler can infer a concrete type for `f(x)`? In other words, the wording in the documentation implies that the call has to be executed for this test, but is that really the case?

[Previous page](https://discourse.julialang.org/t/feature-request-type-assertion-for-compiler-known-type/10305.md?page=1)
