Skip to content

Commit 59ae257

Browse files
committed
Merge pull request #10458 from JuliaLang/jcb/depfpidx
Deprecate conversion to Int for "non-obvious" index types
2 parents f54ba05 + 173bd57 commit 59ae257

File tree

8 files changed

+33
-59
lines changed

8 files changed

+33
-59
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ Library improvements
232232
Deprecated or removed
233233
---------------------
234234

235+
* indexing with Reals that are not subtypes of Integers (Rationals, FloatingPoint, etc.) has been deprecated ([#10458]).
236+
235237
* `push!(A)` has been deprecated, use `append!` instead of splatting arguments to `push!` ([#10400]).
236238

237239
* `names` for composite datatypes has been deprecated and

base/deprecated.jl

+23
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,26 @@ function push!(A)
454454
depwarn("push!(A) has been deprecated", :push!)
455455
A
456456
end
457+
458+
# 10458
459+
to_index_nodep(i::Real) = convert(Int,i)::Int
460+
461+
function to_index(i::Real)
462+
depwarn("indexing with non Integer Reals is deprecated", :to_index)
463+
to_index_nodep(i)
464+
end
465+
466+
function to_index{T<:Real}(r::UnitRange{T})
467+
depwarn("indexing with non Integer UnitRanges is deprecated", :to_index)
468+
to_index_nodep(first(r)):to_index_nodep(last(r))
469+
end
470+
471+
function to_index{T<:Real}(r::StepRange{T})
472+
depwarn("indexing with non Integer StepRanges is deprecated", :to_index)
473+
to_index_nodep(first(r)):to_index_nodep(step(r)):to_index_nodep(last(r))
474+
end
475+
476+
function to_index{T<:Real}(A::AbstractArray{T})
477+
depwarn("indexing with non Integer AbstractArrays is deprecated", :to_index)
478+
Int[to_index_nodep(x) for x in A]
479+
end

base/operators.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,16 @@ end
301301

302302
# convert to integer index
303303
to_index(i::Int) = i
304-
to_index(i::Real) = convert(Int,i)::Int
304+
to_index(i::Integer) = convert(Int,i)::Int
305305
to_index(r::UnitRange{Int}) = r
306306
to_index(r::Range{Int}) = r
307307
to_index(I::UnitRange{Bool}) = find(I)
308308
to_index(I::Range{Bool}) = find(I)
309-
to_index{T<:Real}(r::UnitRange{T}) = to_index(first(r)):to_index(last(r))
310-
to_index{T<:Real}(r::StepRange{T}) = to_index(first(r)):to_index(step(r)):to_index(last(r))
309+
to_index{T<:Integer}(r::UnitRange{T}) = to_index(first(r)):to_index(last(r))
310+
to_index{T<:Integer}(r::StepRange{T}) = to_index(first(r)):to_index(step(r)):to_index(last(r))
311311
to_index(I::AbstractArray{Bool}) = find(I)
312312
to_index(A::AbstractArray{Int}) = A
313-
to_index{T<:Real}(A::AbstractArray{T}) = [to_index(x) for x in A]
313+
to_index{T<:Integer}(A::AbstractArray{T}) = [to_index(x) for x in A]
314314
to_index(i1, i2) = to_index(i1), to_index(i2)
315315
to_index(i1, i2, i3) = to_index(i1), to_index(i2), to_index(i3)
316316
to_index(i1, i2, i3, i4) = to_index(i1), to_index(i2), to_index(i3), to_index(i4)

base/regex.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function match(re::Regex, str::UTF8String, idx::Integer, add_opts::Int32=Int32(0
120120
if !PCRE.exec(re.regex, re.extra, str, idx-1, opts, re.ovec)
121121
return nothing
122122
end
123-
n = length(re.ovec)/3 - 1
123+
n = div(length(re.ovec),3) - 1
124124
mat = SubString(str, re.ovec[1]+1, re.ovec[2])
125125
cap = Union(Void,SubString{UTF8String})[
126126
re.ovec[2i+1] < 0 ? nothing : SubString(str, re.ovec[2i+1]+1, re.ovec[2i+2]) for i=1:n ]

doc/manual/arrays.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ The general syntax for indexing into an n-dimensional array A is::
220220

221221
where each I\_k may be:
222222

223-
1. A scalar value
223+
1. A scalar integer
224224
2. A ``Range`` of the form ``:``, ``a:b``, or ``a:b:c``
225225
3. An arbitrary integer vector, including the empty vector ``[]``
226226
4. A boolean vector

test/arrayops.jl

-5
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,6 @@ function pr8622()
893893
end
894894
@test pr8622() == [0,3,1,0]
895895

896-
# commit b718cbc72e90, getindex(::Number, ::Real)
897-
b718cbc = 5
898-
@test b718cbc[1.0] == 5
899-
@test_throws InexactError b718cbc[1.1]
900-
901896
#6828 - size of specific dimensions
902897
a = Array(Float64, 10)
903898
@test size(a) == (10,)

test/bitarray.jl

+2-45
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ for (sz,T) in allsizes
8383
@check_bit_operation getindex(b1) Bool
8484
@check_bit_operation setindex!(b1, true) T
8585
@check_bit_operation setindex!(b1, false) T
86-
@check_bit_operation setindex!(b1, 1.0) T
8786
end
8887

8988
# linear
@@ -93,7 +92,6 @@ for (sz,T) in allsizes[2:end]
9392
for j = 1:l
9493
@check_bit_operation getindex(b1, j) Bool
9594
end
96-
@check_bit_operation getindex(b1, 100.0) Bool
9795

9896
for j in [0, 1, 63, 64, 65, 127, 128, 129, 191, 192, 193, l-1, l]
9997
@check_bit_operation getindex(b1, 1:j) BitVector
@@ -103,23 +101,17 @@ for (sz,T) in allsizes[2:end]
103101
m1 = j:(l-j)
104102
@check_bit_operation getindex(b1, m1) BitVector
105103
end
106-
@check_bit_operation getindex(b1, 1.0:100.0) BitVector
107104

108105
t1 = find(bitrand(l))
109106
@check_bit_operation getindex(b1, t1) BitVector
110-
@check_bit_operation getindex(b1, float(t1)) BitVector
111107

112108
for j = 1:l
113109
x = rand(Bool)
114110
@check_bit_operation setindex!(b1, x, j) T
115111
end
116112

117-
x = rand(Bool)
118-
@check_bit_operation setindex!(b1, x, 100.0) T
119113
y = rand(0.0:1.0)
120114
@check_bit_operation setindex!(b1, y, 100) T
121-
y = rand(0.0:1.0)
122-
@check_bit_operation setindex!(b1, y, 100.0) T
123115

124116
for j in [1, 63, 64, 65, 127, 128, 129, 191, 192, 193, l-1]
125117
x = rand(Bool)
@@ -139,16 +131,12 @@ for (sz,T) in allsizes[2:end]
139131
@check_bit_operation setindex!(b1, b2, m1) T
140132
end
141133
x = rand(Bool)
142-
@check_bit_operation setindex!(b1, x, 1.0:100.0) T
134+
@check_bit_operation setindex!(b1, x, 1:100) T
143135
b2 = bitrand(100)
144-
@check_bit_operation setindex!(b1, b2, 1.0:100.0) T
136+
@check_bit_operation setindex!(b1, b2, 1:100) T
145137

146138
y = rand(0.0:1.0)
147139
@check_bit_operation setindex!(b1, y, 1:100) T
148-
f2 = float(bitrand(100))
149-
@check_bit_operation setindex!(b1, f2, 1:100) T
150-
f2 = float(bitrand(100))
151-
@check_bit_operation setindex!(b1, f2, 1.0:100.0) T
152140

153141
t1 = find(bitrand(l))
154142
x = rand(Bool)
@@ -158,19 +146,6 @@ for (sz,T) in allsizes[2:end]
158146

159147
y = rand(0.0:1.0)
160148
@check_bit_operation setindex!(b1, y, t1) T
161-
f2 = float(bitrand(length(t1)))
162-
@check_bit_operation setindex!(b1, f2, t1) T
163-
164-
ft1 = float(t1)
165-
x = rand(Bool)
166-
@check_bit_operation setindex!(b1, x, ft1) T
167-
b2 = bitrand(length(t1))
168-
@check_bit_operation setindex!(b1, b2, ft1) T
169-
170-
y = rand(0.0:1.0)
171-
@check_bit_operation setindex!(b1, y, ft1) T
172-
f2 = float(bitrand(length(t1)))
173-
@check_bit_operation setindex!(b1, f2, ft1) T
174149
end
175150

176151
# multidimensional
@@ -208,14 +183,6 @@ for (k1, k2, T) in Task(gen_getindex_data)
208183
# println(typeof(k1), " ", typeof(k2), " ", T) # uncomment to debug
209184
@check_bit_operation getindex(b1, k1, k2) T
210185
@check_bit_operation getindex(b1, k1, k2, 1) T
211-
212-
#@check_bit_operation getindex(b1, float(k1), k2) T
213-
#@check_bit_operation getindex(b1, k1, float(k2)) T
214-
215-
@check_bit_operation getindex(b1, float(k1), float(k2)) T
216-
217-
@check_bit_operation getindex(b1, k1, k2, 1.0) T
218-
#@check_bit_operation getindex(b1, float(k1), float(k2), 1.0) T
219186
end
220187

221188
function gen_setindex_data()
@@ -250,16 +217,6 @@ end
250217
for (b2, k1, k2) in Task(gen_setindex_data)
251218
# println(typeof(b2), " ", typeof(k1), " ", typeof(k2)) # uncomment to debug
252219
@check_bit_operation setindex!(b1, b2, k1, k2) BitMatrix
253-
254-
@check_bit_operation setindex!(b1, float(b2), k1, k2) BitMatrix
255-
@check_bit_operation setindex!(b1, b2, float(k1), k2) BitMatrix
256-
#@check_bit_operation setindex!(b1, b2, k1, float(k2)) BitMatrix
257-
258-
#@check_bit_operation setindex!(b1, float(b2), float(k1), k2) BitMatrix
259-
#@check_bit_operation setindex!(b1, float(b2), k1, float(k2)) BitMatrix
260-
#@check_bit_operation setindex!(b1, b2, float(k1), float(k2)) BitMatrix
261-
262-
@check_bit_operation setindex!(b1, float(b2), float(k1), float(k2)) BitMatrix
263220
end
264221

265222
m1, m2 = rand_m1m2()

test/strings.jl

-3
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,6 @@ gstr = Base.GenericString("12");
13851385
@test convert(Symbol, gstr)==symbol("12")
13861386

13871387
@test getindex(gstr, Bool(1))=='1'
1388-
@test getindex(gstr, 1.0)=='1'
13891388
@test getindex(gstr,Bool(1):Bool(1))=="1"
13901389
@test getindex(gstr,AbstractVector([Bool(1):Bool(1);]))=="1"
13911390

@@ -1399,8 +1398,6 @@ gstr = Base.GenericString("12");
13991398

14001399
@test nextind(AbstractArray([Bool(1):Bool(1);]),1)==2
14011400

1402-
@test checkbounds(gstr,1.0)==true
1403-
14041401
@test ind2chr(gstr,2)==2
14051402

14061403
# issue #10307

0 commit comments

Comments
 (0)