# Why is the returntype of length not always an integer?

**URL:** <https://discourse.julialang.org/t/why-is-the-returntype-of-length-not-always-an-integer/4566>\
**Category:** General Usage\
**Created:** [June 30, 2017, 2:40pm UTC](https://discourse.julialang.org/t/why-is-the-returntype-of-length-not-always-an-integer/4566 "2017-06-30T14:40:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [June 30, 2017, 2:40pm UTC](https://discourse.julialang.org/t/why-is-the-returntype-of-length-not-always-an-integer/4566/1 "2017-06-30T14:40:14Z")

</div>

```julia
julia> code_warntype(length, (Range,))

...

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

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 30, 2017, 3:04pm UTC](https://discourse.julialang.org/t/why-is-the-returntype-of-length-not-always-an-integer/4566/2 "2017-06-30T15:04:12Z")

</div>

It might be, but it might not be inferrable.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 30, 2017, 3:18pm UTC](https://discourse.julialang.org/t/why-is-the-returntype-of-length-not-always-an-integer/4566/3 "2017-06-30T15:18:36Z")

</div>

> [@gdkrmr](#):
>
> `code_warntype(length, (Range,))`

`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?

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [June 30, 2017, 3:30pm UTC](https://discourse.julialang.org/t/why-is-the-returntype-of-length-not-always-an-integer/4566/4 "2017-06-30T15:30:26Z")

</div>

Playing around a little bit:

```julia
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.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [June 30, 2017, 3:43pm UTC](https://discourse.julialang.org/t/why-is-the-returntype-of-length-not-always-an-integer/4566/5 "2017-06-30T15:43:00Z")

</div>

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

```
