Skip to content

Commit e0b02f2

Browse files
committed
add a promote_rule for arrays with different element types
use promote_typeof in mixed-type cat functions
1 parent 713e6f5 commit e0b02f2

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

base/abstractarray.jl

+4-13
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ if _oldstyle_array_vcat_
4040
end
4141
else
4242
function vect(X...)
43-
T = promote_type(map(typeof, X)...)
43+
T = promote_typeof(X...)
4444
T[ X[i] for i=1:length(X) ]
4545
end
4646
end
@@ -616,18 +616,12 @@ vcat{T}(X::T...) = T[ X[i] for i=1:length(X) ]
616616
vcat{T<:Number}(X::T...) = T[ X[i] for i=1:length(X) ]
617617

618618
function vcat(X::Number...)
619-
T = Bottom
620-
for x in X
621-
T = promote_type(T,typeof(x))
622-
end
619+
T = promote_typeof(X...)
623620
hvcat_fill(Array(T,length(X)), X)
624621
end
625622

626623
function hcat(X::Number...)
627-
T = Bottom
628-
for x in X
629-
T = promote_type(T,typeof(x))
630-
end
624+
T = promote_typeof(X...)
631625
hvcat_fill(Array(T,1,length(X)), X)
632626
end
633627

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

875869
function hvcat(rows::(Int...), xs::Number...)
876-
T = typeof(xs[1])
877-
for i = 2:length(xs)
878-
T = promote_type(T,typeof(xs[i]))
879-
end
870+
T = promote_typeof(xs...)
880871
typed_hvcat(T, rows, xs...)
881872
end
882873

base/array.jl

+2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x
243243
convert{T,n,S}(::Type{Array{T}}, x::Array{S,n}) = convert(Array{T,n}, x)
244244
convert{T,n,S}(::Type{Array{T,n}}, x::Array{S,n}) = copy!(similar(x,T), x)
245245

246+
promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type(T,S),n}
247+
246248
function collect(T::Type, itr)
247249
if applicable(length, itr)
248250
# when length() isn't defined this branch might pollute the

0 commit comments

Comments
 (0)