Skip to content

Commit fef31cd

Browse files
authored
Type-inference improvement in reduce over Ones (#291)
* Type-inference improvement in reduce over Ones * reduce_first on the value
1 parent 5abd5f6 commit fef31cd

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FillArrays"
22
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
3-
version = "1.6.0"
3+
version = "1.6.1"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/fillalgebra.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ end
353353
-(a::Zeros) = a
354354
-(a::AbstractFill) = Fill(-getindex_value(a), size(a))
355355

356+
# special-cased for type-stability, as Ones + Ones is not a Ones
357+
Base.reduce_first(::typeof(+), x::Ones) = Fill(Base.reduce_first(+, getindex_value(x)), axes(x))
356358

357359
function +(a::Zeros{T}, b::Zeros{V}) where {T, V} # for disambiguity
358360
promote_shape(a,b)

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,9 @@ end
825825
@test diff(Ones{Float64}(10)) Zeros{Float64}(9)
826826
@test_throws UndefKeywordError cumsum(Fill(1,1,5))
827827

828+
@test @inferred(sum([Ones(4)])) Fill(1.0, 4)
829+
@test @inferred(sum([Trues(4)])) Fill(1, 4)
830+
828831
@testset "infinite arrays" begin
829832
r = InfiniteArrays.OneToInf()
830833
A = Ones{Int}((r,))

0 commit comments

Comments
 (0)