Why is the returntype of length not always an integer?


#1
julia> code_warntype(length, (Range,))

...

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


#2

It might be, but it might not be inferrable.


#3

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?


#4

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.


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