diff --git a/base/array.jl b/base/array.jl index 8432c78f2f66c..47383ebc6a6f1 100644 --- a/base/array.jl +++ b/base/array.jl @@ -466,20 +466,21 @@ function push!(a::Array{Any,1}, item::ANY) return a end -function append!{T}(a::Array{T,1}, items::AbstractVector) - if is(T,None) - error(_grow_none_errmsg) - end +function append!{T}(a::Array{T,1}, items::AbstractVector{T}) n = length(items) ccall(:jl_array_grow_end, Void, (Any, Uint), a, n) copy!(a, length(a)-n+1, items, 1, n) return a end -function prepend!{T}(a::Array{T,1}, items::AbstractVector) +function append!{T}(a::Array{T,1}, items::AbstractVector) if is(T,None) error(_grow_none_errmsg) end + append!(a, convert(Array{T,1}, items)) +end + +function prepend!{T}(a::Array{T,1}, items::AbstractVector{T}) n = length(items) ccall(:jl_array_grow_beg, Void, (Any, Uint), a, n) if a === items @@ -490,6 +491,13 @@ function prepend!{T}(a::Array{T,1}, items::AbstractVector) return a end +function prepend!{T}(a::Array{T,1}, items::AbstractVector) + if is(T,None) + error(_grow_none_errmsg) + end + prepend!(a, convert(Array{T,1}, items)) +end + function resize!(a::Vector, nl::Integer) l = length(a) if nl > l @@ -589,9 +597,7 @@ function deleteat!(a::Vector, inds) return a end -const _default_splice = [] - -function splice!(a::Vector, i::Integer, ins::AbstractArray=_default_splice) +function splice!{T}(a::Array{T,1}, i::Integer, ins::AbstractVector{T}=T[]) v = a[i] m = length(ins) if m == 0 @@ -607,7 +613,10 @@ function splice!(a::Vector, i::Integer, ins::AbstractArray=_default_splice) return v end -function splice!{T<:Integer}(a::Vector, r::UnitRange{T}, ins::AbstractArray=_default_splice) +splice!{T}(a::Array{T,1}, i::Integer, ins::AbstractVector) = + splice!(a, i, convert(Array{T,1}, ins)) + +function splice!{T,N<:Integer}(a::Array{T,1}, r::UnitRange{N}, ins::AbstractVector{T}=T[]) v = a[r] m = length(ins) if m == 0 @@ -642,6 +651,9 @@ function splice!{T<:Integer}(a::Vector, r::UnitRange{T}, ins::AbstractArray=_def return v end +splice!{T,N<:Integer}(a::Array{T,1}, r::UnitRange{N}, ins::AbstractVector) = + splice!(a, r, convert(Array{T,1}, ins)) + function empty!(a::Vector) ccall(:jl_array_del_end, Void, (Any, Uint), a, length(a)) return a diff --git a/test/arrayops.jl b/test/arrayops.jl index b6b564fc4f152..2c6f0b3297d26 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -893,3 +893,30 @@ function i7197() ind2sub(size(S), 5) end @test i7197() == (2,2) + +# issue #7642 +A = [1,2,3] +B = ["hey"] +@test_throws MethodError append!(A,B) +@test length(A) == 3 + +@test_throws MethodError prepend!(A,B) +@test length(A) == 3 + +@test_throws MethodError splice!(A,1,B) +@test length(A) == 3 +@test_throws MethodError splice!(A,2,B) +@test length(A) == 3 +@test_throws MethodError splice!(A,3,B) +@test length(A) == 3 + +@test_throws MethodError splice!(A,1:3,B) +@test length(A) == 3 + +B = [1.0, 1.2, 1.5] +@test_throws InexactError append!(A,B) +@test length(A) == 3 +@test_throws InexactError prepend!(A,B) +@test length(A) == 3 +@test_throws InexactError splice!(A,1:3,B) +@test length(A) == 3