@@ -58,6 +58,9 @@ Valid invocations of range are:
5858* Call `range` with any three of `start`, `step`, `stop`, `length`.
5959* Call `range` with two of `start`, `stop`, `length`. In this case `step` will be assumed
6060 to be one. If both arguments are Integers, a [`UnitRange`](@ref) will be returned.
61+ * Call `range` with one of `stop` or `length`. `start` and `step` will be assumed to be one.
62+
63+ See Extended Help for additional details on the returned type.
6164
6265# Examples
6366```jldoctest
@@ -87,6 +90,15 @@ julia> range(stop=10, step=1, length=5)
8790
8891julia> range(start=1, step=1, stop=10)
89921:1:10
93+
94+ julia> range(; length = 10)
95+ Base.OneTo(10)
96+
97+ julia> range(; stop = 6)
98+ Base.OneTo(6)
99+
100+ julia> range(; stop = 6.5)
101+ 1.0:1.0:6.0
90102```
91103If `length` is not specified and `stop - start` is not an integer multiple of `step`, a range that ends before `stop` will be produced.
92104```jldoctest
@@ -103,6 +115,23 @@ To avoid this induced overhead, see the [`LinRange`](@ref) constructor.
103115!!! compat "Julia 1.7"
104116 The versions without keyword arguments and `start` as a keyword argument
105117 require at least Julia 1.7.
118+
119+ !!! compat "Julia 1.8"
120+ The versions with `stop` as a sole keyword argument,
121+ or `length` as a sole keyword argument require at least Julia 1.8.
122+
123+
124+ # Extended Help
125+
126+ `range` will produce a `Base.OneTo` when the arguments are Integers and
127+ * Only `length` is provided
128+ * Only `stop` is provided
129+
130+ `range` will produce a `UnitRange` when the arguments are Integers and
131+ * Only `start` and `stop` are provided
132+ * Only `length` and `stop` are provided
133+
134+ A `UnitRange` is not produced if `step` is provided even if specified as one.
106135"""
107136function range end
108137
@@ -115,8 +144,8 @@ range(;start=nothing, stop=nothing, length::Union{Integer, Nothing}=nothing, ste
115144 _range (start, step, stop, length)
116145
117146_range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Nothing ) = range_error (start, step, stop, len)
118- _range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Any ) = range_error (start, step, stop, len)
119- _range (start:: Nothing , step:: Nothing , stop:: Any , len:: Nothing ) = range_error (start, step, stop, len )
147+ _range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Any ) = range_length ( len)
148+ _range (start:: Nothing , step:: Nothing , stop:: Any , len:: Nothing ) = range_stop ( stop)
120149_range (start:: Nothing , step:: Nothing , stop:: Any , len:: Any ) = range_stop_length (stop, len)
121150_range (start:: Nothing , step:: Any , stop:: Nothing , len:: Nothing ) = range_error (start, step, stop, len)
122151_range (start:: Nothing , step:: Any , stop:: Nothing , len:: Any ) = range_error (start, step, stop, len)
@@ -131,6 +160,14 @@ _range(start::Any , step::Any , stop::Nothing, len::Any ) = range_start
131160_range (start:: Any , step:: Any , stop:: Any , len:: Nothing ) = range_start_step_stop (start, step, stop)
132161_range (start:: Any , step:: Any , stop:: Any , len:: Any ) = range_error (start, step, stop, len)
133162
163+ # Length as the only argument
164+ range_length (len:: Integer ) = OneTo (len)
165+
166+ # Stop as the only argument
167+ range_stop (stop) = range_start_stop (oneunit (stop), stop)
168+ range_stop (stop:: Integer ) = range_length (stop)
169+
170+ # Stop and length as the only argument
134171range_stop_length (a:: Real , len:: Integer ) = UnitRange {typeof(a)} (oftype (a, a- len+ 1 ), a)
135172range_stop_length (a:: AbstractFloat , len:: Integer ) = range_step_stop_length (oftype (a, 1 ), a, len)
136173range_stop_length (a, len:: Integer ) = range_step_stop_length (oftype (a- a, 1 ), a, len)
0 commit comments