# Force type stability (over block of code)

**URL:** <https://discourse.julialang.org/t/force-type-stability-over-block-of-code/86501>\
**Category:** Performance\
**Tags:** type-stability\
**Created:** [August 29, 2022, 2:30pm UTC](https://discourse.julialang.org/t/force-type-stability-over-block-of-code/86501 "2022-08-29T14:30:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)\
**Post date:** [August 29, 2022, 2:30pm UTC](https://discourse.julialang.org/t/force-type-stability-over-block-of-code/86501/1 "2022-08-29T14:30:40Z")

</div>

Hi Julianners,  
I saw many attempt on type table block.

> <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).

> **[GitHub - JunoLab/Traceur.jl](https://github.com/JunoLab/Traceur.jl)**
>
> Contribute to JunoLab/Traceur.jl development by creating an account on GitHub.

`@code_warntype` to trace them.

But in the end I don’t find what is the way to force type stability over a specific block. I want the code to stop or do a warn message any time it uses Any or Any like types.

I want to make sure we don’t waste resources. Do you know something that forbid type instability?

Best!

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [August 29, 2022, 2:41pm UTC](https://discourse.julialang.org/t/force-type-stability-over-block-of-code/86501/2 "2022-08-29T14:41:43Z")

</div>

[JET.@test\_opt](https://aviatesk.github.io/JET.jl/stable/optanalysis/#JET.@test_opt) can test that a function call is never hits dynamic dispatch, which is very close to what you are asking for.
