Skip to content

Commit 4c17ea3

Browse files
committed
reshape(::Range, dims) yields an Array, not a ReshapedArray
1 parent 2624ed8 commit 4c17ea3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

base/reshapedarray.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ function ReshapedArray(parent::AbstractArray, indexes::(ReshapeIndex...), dims)
2323
ReshapedArray{eltype(parent),length(dims),typeof(parent),typeof(indexes)}(parent, indexes, dims)
2424
end
2525

26+
# Since ranges are immutable and are frequently used to build arrays, treat them as a special case.
27+
function reshape(a::Range, dims::Dims)
28+
if prod(dims) != length(a)
29+
throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(length(a))"))
30+
end
31+
A = Array(eltype(a), dims)
32+
k = 0
33+
for item in a
34+
A[k+=1] = item
35+
end
36+
A
37+
end
38+
2639
reshape(parent::AbstractArray, dims::Dims) = reshape((parent, linearindexing(parent)), dims)
2740

2841
function reshape(p::(AbstractArray,LinearSlow), dims::Dims)

0 commit comments

Comments
 (0)