Why type instability?

I would like a feature like this as well… Like an assert. @type_stable_assert :stuck_out_tongue:

1 Like

The hard part is finding a set of static rules for a subset of Julia, but it does seem like it might be possible to find such a rule set and let people declare sections of code to follow those rules and accordingly get guaranteed tight type bounds and compile-time type checking. Definitely an interesting area for future work.

5 Likes

On the other hand, you can type assert return types, fun(x)::Int, which automatically makes the function type stable. This is the way C guarantees type stability.

If you mean that this

function fun(x)::Int 
...
end

asserts the return-type, then you’re wrong (otherwise ignore me!). It converts the type:

julia> function f(x)::Int
       x
       end                                                                                                                                                              
f (generic function with 1 method)                                                                                                                                      

julia> f(Int32(3))                                                                                                                                                      
3                                                                                                                                                                       

julia> Int32(3)::Int                                                                                                                                                    
ERROR: TypeError: typeassert: expected Int64, got Int32                                                                                                                 

My point is that it guarantees type-stability. Wherever you call fun you are sure the return type is an Int. Also, it only converts the type if it is “convertable”. fun("hello") throws an error.