Skip to content

Commit bffeedb

Browse files
Evizeronalimilan
authored andcommitted
Add missing convert(Vector, ...) and convert(Matrix, ...) methods (#17848)
This commit will allow a user to call: convert(Vector, my_abstractvector_of_eltype_Foo) convert(Matrix, my_abstractmatrix_of_eltype_Foo) which is useful because - for the case that the variable is a subarray of the same dimensionality - for consistency with PR #17066
1 parent 6d179b3 commit bffeedb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

base/array.jl

+3
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ end
226226

227227
## Conversions ##
228228

229+
convert{T}(::Type{Vector}, x::AbstractVector{T}) = convert(Vector{T}, x)
230+
convert{T}(::Type{Matrix}, x::AbstractMatrix{T}) = convert(Matrix{T}, x)
231+
229232
convert{T,n}(::Type{Array{T}}, x::Array{T,n}) = x
230233
convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x
231234

test/abstractarray.jl

+12
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,19 @@ function test_primitives{T}(::Type{T}, shape, ::Type{TestAbstractArray})
405405

406406
# convert{T, N}(::Type{Array}, A::AbstractArray{T, N})
407407
X = [1:10...]
408+
Y = [1 2; 3 4]
408409
@test convert(Array, X) == X
410+
@test convert(Array, Y) == Y
411+
412+
# convert{T}(::Type{Vector}, A::AbstractVector{T})
413+
@test convert(Vector, X) == X
414+
@test convert(Vector, view(X, 2:4)) == [2,3,4]
415+
@test_throws MethodError convert(Vector, Y)
416+
417+
# convert{T}(::Type{Matrix}, A::AbstractMatrix{T})
418+
@test convert(Matrix, Y) == Y
419+
@test convert(Matrix, view(Y, 1:2, 1:2)) == Y
420+
@test_throws MethodError convert(Matrix, X)
409421
end
410422

411423
let

0 commit comments

Comments
 (0)