Julia: range(π, stop = π, length = 1) cannot be collected

Created on 16 Jul 2020  ·  4Comments  ·  Source: JuliaLang/julia

julia> collect(range(π, stop=π, length=1))
ERROR: MethodError: no method matching Irrational{:π}(::Float64)
...

The problem seems to be that range(π, stop=π, length=1) isa LinRange{Irrational{:π}}, which, when iterated, causes a call to T(::Float64) with T===Irrational{:π} in unsage_getindex here:
https://github.com/JuliaLang/julia/blob/353e1296ae3927eef07a5476554fb641dd1a287c/base/range.jl#L689

Not sure what the correct fix would be

Most helpful comment

I'm not sure this needs to work.

All 4 comments

I'm not sure this needs to work.

Care to elaborate? collect(range(2π, stop=2π, length=1)) and collect(range(1, stop=1, length=1)) do work. Isn't the current behavior surprising?

EDIT: in other words, isn't this an instance of implementation details leaking over into the behavior of ranges?

is a Float64, not an Irrational. Irrational can't support all operations, and that's just a tradeoff we have accepted. I guess we could special-case the first element to return the start point instead of doing arithmetic, but it doesn't seem worth it just to allow 1-element ranges of Irrationals.

I see, thanks for the explanation! There seems to be a powerful consensus here, so closing.

Was this page helpful?
0 / 5 - 0 ratings