# Why doesn't \`@nospecialize\` work in this example?

**URL:** <https://discourse.julialang.org/t/why-doesnt-nospecialize-work-in-this-example/88695>\
**Category:** General Usage\
**Created:** [October 13, 2022, 10:24pm UTC](https://discourse.julialang.org/t/why-doesnt-nospecialize-work-in-this-example/88695 "2022-10-13T22:24:31Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 17, 2022, 5:39pm UTC](https://discourse.julialang.org/t/why-doesnt-nospecialize-work-in-this-example/88695/6 "2022-10-17T17:39:38Z")

</div>

`@nospecialize` is targeted at the callee, but the caller is still allowed to try and guess what type is returned. It therefore doesn’t block inference directly, though loss of specialization can indirectly block inference. [https://github.com/JuliaLang/julia/pull/41931](https://github.com/JuliaLang/julia/pull/41931)

You might need `Base.inferencebarrier`:

```julia
julia> do_something_with_trait(x) = has_trait(typeof(Base.inferencebarrier(x))) ? "success" : "failure"
do_something_with_trait (generic function with 1 method)

julia> do_something_with_trait(Foo(1))
"success"

julia> do_something_with_trait(Foo("a"))
"success"

julia> methodinstances(has_trait)
2-element Vector{Core.MethodInstance}:
 MethodInstance for has_trait(::Type)
 MethodInstance for has_trait(::DataType)

```

---

_[View the full topic](https://discourse.julialang.org/t/why-doesnt-nospecialize-work-in-this-example/88695)._
