Why is the returntype of length not always an integer?

julia> code_warntype(length, (Range,))

...

 end::Union{AbstractArray{T<:Number,2},AbstractString,Number}

It might be, but it might not be inferrable.

Range isn’t a concrete type, so Julia won’t ever call a length(::Range) method — it’ll specialize it to the exact subtype first. The output doesn’t reflect anything that’ll ever get called. Perhaps this should be an error?

Playing around a little bit:

julia> Base.unsafe_length(1:1:2)
2

julia> @code_warntype Base.unsafe_length(1:1:2)
ERROR: inference not successful
 in #code_typed#18(::Bool, ::Function, ::Any, ::Any) at ./reflection.jl:412
 in code_warntype(::Base.TTY, ::Function, ::Any) at ./interactiveutil.jl:265
 in code_warntype(::Function, ::Any) at ./interactiveutil.jl:286

Edit: a restart of Julia made this error disappear.

julia> @code_warntype length(1:10)
  ...
  end::Int64