<p dir="auto">There should be gramma/macro in Julia to declare a function as type stable (or no box). Like <code class="notranslate">stable function(x) = ...</code>, so that the program can break down/warn immediately when unexpected allocations occur.</p>
<p dir="auto">1). Speed and type stability are defining features of Julia, it should allow enforced type stable functions. I’d like to cite Tim’s comment in his <a href="https://arxiv.org/abs/1712.03112" rel="nofollow">GPU paper</a></p>
<blockquote>
<p dir="auto">The most remarkable aspect of the language and its main implementation is speed: carefully written Julia code performs exceptionally well on traditional microprocessors, approaching the speed of code written in statically-compiled languages like C or FORTRAN.<br>
Julia’s competitive performance originates from clever language design that avoids the typical compilation and execution uncertainties associated with dynamic languages.</p>
</blockquote>
<p dir="auto">In many cases, I prefer my program to error automatically if some of my functions (e.g. designed to be used inside a loop) are type unstable (or have to use a Box).</p>
<p dir="auto">2). Julia is a powerful language that can support GPU and TPU programming easily. However, kernel functions on these devices can not allocate memories, meanwhile hard to debug. Half of my debugging experiences are related to type instability. In this case, proactive type stability assurance is extremely important for noticing the bug immediately.</p>
<p dir="auto">One may argue <code class="notranslate">@code_warntype</code> is enough, but it is not true. I think David Buchana gives a perfect <a href="https://discourse.julialang.org/t/drastic-performance-hit-matrix-multiply-different-types-internal-cast-julia-vs-numpy/17120" rel="nofollow">example</a>: <code class="notranslate">rand([0,1],10)</code> vs <code class="notranslate">rand([0.,1.],10)</code> make the code like 30 times slower due to not calling the BLAS. This kind of debugging is often time consuming: you should first notice your program is running slow, benchmark to verify it is slower than before, and profile to locate where it slows down, then <code class="notranslate">@code_warntype</code> can be helpful. So <code class="notranslate">@code_warntype</code> can only help in the final stage.</p>
<p dir="auto">Given the importance of type stability, I would like to propose a new language construct that would allow semi-static checking of type stable blocks. In the module I am developing, I am frequently using this type of construct</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function f(a)
 # Type stability
@stabilize m::Matrix{Float64} = a.m

 # Compute things with m

end
..."><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">f</span>(a)
 <span class="pl-c"><span class="pl-c">#</span> Type stability</span>
<span class="pl-c1">@stabilize</span> m<span class="pl-k">::</span><span class="pl-c1">Matrix{Float64}</span> <span class="pl-k">=</span> a<span class="pl-k">.</span>m

 <span class="pl-c"><span class="pl-c">#</span> Compute things with m</span>

<span class="pl-k">end</span>
<span class="pl-k">...</span></pre></div>
<p dir="auto">I know there are:</p>
<ol dir="auto">
<li>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)</li>
<li>Tools like Lint or TypeCheck, and the macro <code class="notranslate">@code_warntype</code> that are useful to see/check whether the written code is type stable</li>
</ol>
<p dir="auto">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]):</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="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
..."><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">f</span>(a)
 <span class="pl-c"><span class="pl-c">#</span> Type stability</span>
<span class="pl-c1">@stabilize</span> m<span class="pl-k">::</span><span class="pl-c1">Matrix{Float64}</span> <span class="pl-k">=</span> a<span class="pl-k">.</span>m

 stable <span class="pl-k">begin</span>
  <span class="pl-c"><span class="pl-c">#</span> Compute things - compilation error (or warning) if there is some type instability within the code</span>
 <span class="pl-k">end</span>

<span class="pl-k">end</span>
<span class="pl-k">...</span></pre></div>
<p dir="auto">Another syntactic possibility would be to use pragmas <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="36729112" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/7449" data-hovercard-type="issue" data-hovercard-url="/JuliaLang/julia/issues/7449/hovercard" href="https://github.com/JuliaLang/julia/issues/7449">#7449</a> (if they make it in the language).</p>