-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Range construction with colon, one, and zero #41840
Conversation
Copied over from #41840: I'm not sure if there's already an issue on that - the range-from-one and range-from-zero could be done very elegantly if we had precision-independent representations for one and zero in the language, with separate types for one and zero (along the lines of https://github.com/perrutquist/Zeros.jl), like we have with This approach has worked nicely for constants like If we had special types for one and zero, let's call them Zero():n == Base.ZeroTo(n)
One():n == Base.OneTo(n) (We currently don't have That would be even cleaner than A convenience Syntax for zero-based ranges should wait until we have something like |
What is the difference between |
One would be a number that can be used in calculations (with resulting optimizations), so |
this seems too clever, and thus unlikely to work very well in practice (see discussion in #41840) |
I think the main discussion occured in #41853. This is 41840. This pull request is quite distinct from #41853 as this is a pull request extending existing methods in to work on existing types in Here is an example of how easy accidental type promotion is in Julia at the moment. julia> stop = UInt8(5)
0x05
julia> 1:stop
1:5
julia> typeof(1:stop) # Where did the Int64 come from? How clear is this to a new user of Julia?
UnitRange{Int64}
julia> one(stop):stop # This is awkward and redundant syntax, but ...
0x01:0x05
julia> typeof(ans) # ... it does not make Int64s appear from nowhere.
UnitRange{UInt8} In contrast, with this pull request, observe how natural and intuitive the following code looks and functions. julia> (:)(::typeof(one), stop::T) where {T<:Integer} = Base.OneTo(stop)
: (generic function with 1 method)
julia> stop = UInt8(5)
0x05
julia> one:stop
Base.OneTo(5)
julia> typeof(ans) # No accidental type promotion
Base.OneTo{UInt8} Accidental type promotion leads to bugs such as #39808 in Line 679 in 6fb3558
Rather than introducing abstract algebraic concepts and types into Thank you for the consideration. |
I program only with malicious intent 👺 |
As suiggested in JuliaLang/julia#41840
Currently in
Base
, we have the methodsone
andzero
. Implicitly, we also then have the typestypeof(one)
andtypeof(zero)
. In this pull request, we dispatch(:)
ontypeof(one)
andtypeof(zero)
.When
one
is used asstart
,start
isone(stop)
. Likewise forzero
.When
one
is used asstop
,stop
isone(start)
. Likewise forzero
.When
one
is used asstep
, it is equivalent tostart:stop
.one:(stop::T) where {T <: Integer}
returns aBase.OneTo
.It's not clear to me where the best place to document this behavior is at the moment.
Concepts here were developed in during a conversation with @oschulz .