Skip to content

Commit 772745b

Browse files
authored
Forward to_shape for AbstractOneTo to Integer method (#57990)
This allows custom `Integer` types to add methods to `to_shape` and have the behavior of `OneTo` be altered accordingly.
1 parent b265dba commit 772745b

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

base/abstractarray.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,9 @@ to_shape(dims::DimsOrInds) = map(to_shape, dims)::DimsOrInds
838838
# each dimension
839839
to_shape(i::Int) = i
840840
to_shape(i::Integer) = Int(i)
841-
to_shape(r::AbstractOneTo) = Int(last(r))
841+
to_shape(r::AbstractOneTo) = _to_shape(last(r))
842+
_to_shape(x::Integer) = to_shape(x)
843+
_to_shape(x) = Int(x)
842844
to_shape(r::AbstractUnitRange) = r
843845

844846
"""

test/abstractarray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,5 +2246,7 @@ end
22462246
b = similar(A, SizedArrays.SOneTo(1), 2, Base.OneTo(2))
22472247
@test b isa Array{Int, 3}
22482248
@test size(b) == (1, 2, 2)
2249+
2250+
@test_throws "no method matching $Int(::$Infinity)" similar(ones(2), OneToInf())
22492251
end
22502252
end

test/testhelpers/InfiniteArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Define an `AbstractInfUnitRange` that behaves like `1:∞`, with the added
3737
distinction that the limits are guaranteed (by the type system) to
3838
be 1 and ∞.
3939
"""
40-
struct OneToInf{T<:Integer} <: AbstractUnitRange{T} end
40+
struct OneToInf{T<:Integer} <: Base.AbstractOneTo{T} end
4141

4242
OneToInf() = OneToInf{Int}()
4343

0 commit comments

Comments
 (0)