Skip to content

Commit 1c0304f

Browse files
committed
Fix #97 by adding promote_rules
1 parent 10b4c00 commit 1c0304f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/FillArrays.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
66
+, -, *, /, \, diff, sum, cumsum, maximum, minimum, sort, sort!,
77
any, all, axes, isone, iterate, unique, allunique, permutedims, inv,
88
copy, vec, setindex!, count, ==, reshape, _throw_dmrs, map, zero,
9-
show, view, in, mapreduce, one, reverse, promote_op
9+
show, view, in, mapreduce, one, reverse, promote_op, promote_rule
1010

1111
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
1212
dot, norm2, norm1, normInf, normMinusInf, normp, lmul!, rmul!, diagzero, AdjointAbsVec, TransposeAbsVec,
@@ -34,6 +34,7 @@ const AbstractFillVecOrMat{T} = Union{AbstractFillVector{T},AbstractFillMatrix{T
3434

3535
==(a::AbstractFill, b::AbstractFill) = axes(a) == axes(b) && getindex_value(a) == getindex_value(b)
3636

37+
3738
@inline function _fill_getindex(F::AbstractFill, kj::Integer...)
3839
@boundscheck checkbounds(F, kj...)
3940
getindex_value(F)
@@ -273,6 +274,14 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
273274
copy(F::$Typ) = F
274275

275276
getindex(F::$Typ{T,0}) where T = getindex_value(F)
277+
278+
promote_rule(::Type{$Typ{T, N, Axes}}, ::Type{$Typ{V, N, Axes}}) where {T,V,N,Axes} = $Typ{promote_type(T,V),N,Axes}
279+
function convert(::Type{$Typ{T,N,Axes}}, A::$Typ{V,N,Axes}) where {T,V,N,Axes}
280+
convert(T, getindex_value(A)) # checks that the types are convertible
281+
$Typ{T,N,Axes}(axes(A))
282+
end
283+
convert(::Type{$Typ{T,N}}, A::$Typ{V,N,Axes}) where {T,V,N,Axes} = convert($Typ{T,N,Axes}, A)
284+
convert(::Type{$Typ{T}}, A::$Typ{V,N,Axes}) where {T,V,N,Axes} = convert($Typ{T,N,Axes}, A)
276285
end
277286
end
278287

@@ -284,6 +293,8 @@ for TYPE in (:Fill, :AbstractFill, :Ones, :Zeros), STYPE in (:AbstractArray, :Ab
284293
end
285294
end
286295

296+
promote_rule(::Type{<:AbstractFill{T, N, Axes}}, ::Type{<:AbstractFill{V, N, Axes}}) where {T,V,N,Axes} = Fill{promote_type(T,V),N,Axes}
297+
287298
"""
288299
fillsimilar(a::AbstractFill, axes)
289300

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ include("infinitearrays.jl")
194194
A = FillArrays.RectDiagonal(Int[], (1:0, 1:4))
195195
@test !(0 in A)
196196
end
197+
198+
@testset "promotion" begin
199+
Z = Zeros{Int}(5)
200+
Zf = Zeros(5)
201+
O = Ones{Int}(5)
202+
Of = Ones{Float64}(5)
203+
@test [Z,O] isa Vector{Fill{Int,1,Tuple{Base.OneTo{Int}}}}
204+
@test [Z,Of] isa Vector{Fill{Float64,1,Tuple{Base.OneTo{Int}}}}
205+
@test [O,O] isa Vector{Ones{Int,1,Tuple{Base.OneTo{Int}}}}
206+
@test [O,Of] isa Vector{Ones{Float64,1,Tuple{Base.OneTo{Int}}}}
207+
@test [Z,Zf] isa Vector{Zeros{Float64,1,Tuple{Base.OneTo{Int}}}}
208+
209+
@test convert(Ones{Int}, Of) convert(Ones{Int,1}, Of) convert(typeof(O), Of) O
210+
@test convert(Zeros{Int}, Zf) convert(Zeros{Int,1}, Zf) convert(typeof(Z), Zf) Z
211+
212+
@test_throws MethodError convert(Zeros{SVector{2,Int}}, Zf)
213+
end
197214
end
198215

199216
@testset "indexing" begin

0 commit comments

Comments
 (0)