Skip to content

Commit f0aedc6

Browse files
authored
Merge pull request #20988 from Sacha0/nixcolonambig
resolve ambiguity between colon methods
2 parents 6051590 + 416d85b commit f0aedc6

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

base/range.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ Range operator. `a:b` constructs a range from `a` to `b` with a step size of 1,
3838
is similar but uses a step size of `s`. These syntaxes call the function `colon`. The colon
3939
is also used in indexing to select whole dimensions.
4040
"""
41-
function colon{T}(start::T, step, stop::T)
41+
colon{T}(start::T, step, stop::T) = _colon(start, step, stop)
42+
colon{T<:Real}(start::T, step, stop::T) = _colon(start, step, stop)
43+
# without the second method above, the first method above is ambiguous with
44+
# colon{A<:Real,C<:Real}(start::A, step, stop::C)
45+
function _colon{T}(start::T, step, stop::T)
4246
T′ = typeof(start+step)
4347
StepRange(convert(T′,start), step, convert(T′,stop))
4448
end

test/ranges.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,14 @@ let r = linspace(1.0, 3+im, 4)
894894
@test r[4] === 3.0+im
895895
end
896896

897+
# ambiguity between colon methods (#20988)
898+
struct NotReal; val; end
899+
Base.:+(x, y::NotReal) = x + y.val
900+
Base.zero(y::NotReal) = zero(y.val)
901+
Base.rem(x, y::NotReal) = rem(x, y.val)
902+
Base.isless(x, y::NotReal) = isless(x, y.val)
903+
@test colon(1, NotReal(1), 5) isa StepRange{Int,NotReal}
904+
897905
# dimensional correctness:
898906
isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
899907
using TestHelpers.Furlong

0 commit comments

Comments
 (0)