Skip to content

Commit

Permalink
add a promote_rule for arrays with different element types
Browse files Browse the repository at this point in the history
use promote_typeof in mixed-type cat functions
  • Loading branch information
JeffBezanson committed Feb 9, 2015
1 parent 713e6f5 commit e0b02f2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
17 changes: 4 additions & 13 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if _oldstyle_array_vcat_
end
else
function vect(X...)
T = promote_type(map(typeof, X)...)
T = promote_typeof(X...)
T[ X[i] for i=1:length(X) ]
end
end
Expand Down Expand Up @@ -616,18 +616,12 @@ vcat{T}(X::T...) = T[ X[i] for i=1:length(X) ]
vcat{T<:Number}(X::T...) = T[ X[i] for i=1:length(X) ]

function vcat(X::Number...)
T = Bottom
for x in X
T = promote_type(T,typeof(x))
end
T = promote_typeof(X...)
hvcat_fill(Array(T,length(X)), X)
end

function hcat(X::Number...)
T = Bottom
for x in X
T = promote_type(T,typeof(x))
end
T = promote_typeof(X...)
hvcat_fill(Array(T,1,length(X)), X)
end

Expand Down Expand Up @@ -873,10 +867,7 @@ function typed_hvcat(T::Type, rows::(Int...), xs::Number...)
end

function hvcat(rows::(Int...), xs::Number...)
T = typeof(xs[1])
for i = 2:length(xs)
T = promote_type(T,typeof(xs[i]))
end
T = promote_typeof(xs...)
typed_hvcat(T, rows, xs...)
end

Expand Down
2 changes: 2 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x
convert{T,n,S}(::Type{Array{T}}, x::Array{S,n}) = convert(Array{T,n}, x)
convert{T,n,S}(::Type{Array{T,n}}, x::Array{S,n}) = copy!(similar(x,T), x)

promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type(T,S),n}

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Feb 9, 2015

Member

I'm sort of shocked we didn't have this already.

function collect(T::Type, itr)
if applicable(length, itr)
# when length() isn't defined this branch might pollute the
Expand Down

0 comments on commit e0b02f2

Please sign in to comment.