Skip to content

Commit

Permalink
fix bugs, thanks to mcabbott
Browse files Browse the repository at this point in the history
  • Loading branch information
nsajko committed Dec 24, 2024
1 parent 079ae3f commit 39fed7d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
6 changes: 4 additions & 2 deletions base/generator_eltype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ function eltype(::Type{Generator{A, typeof(identity)}}) where {A}
eltype(A)
end

function eltype(::Type{Generator{A, Fix1{typeof(getindex), B}}} where {A}) where {B}
function eltype(::Type{Generator{A, Fix1{typeof(getindex), B}}}) where {A, B}
if B <: Type
# a user may overload `getindex(user_type)` to return a non-`Vector` `AbstractVector`
Any
elseif (eltype(A) == keytype(B)) || ((eltype(A) <: Integer) && (keytype(B) <: Integer))
valtype(B)
else
eltype(B)
Any
end
end

Expand Down
16 changes: 10 additions & 6 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1017,17 +1017,21 @@ end

@testset "`eltype` for `Generator` involving `Fix` and `getindex`/`getfield` (issue #41519)" begin
@testset "correct `eltype`" begin
f = Base.Fix1(getindex, Int)
i = [3, 7]
r = map(f, i)
t = Iterators.map(f, i)
@test eltype(r) <: @inferred eltype(t)
@test (@inferred Base.IteratorEltype(t)) isa Base.IteratorEltype
for (f, i) (
(Base.Fix1(getindex, Int), [3, 7]),
(Base.Fix1(getindex, [3, 7]), [[1 2], [2 1]]),
)
r = map(f, i)
t = Iterators.map(f, i)
@test eltype(r) <: @inferred eltype(t)
@test (@inferred Base.IteratorEltype(t)) isa Base.IteratorEltype
end
end
@testset "precise `eltype`" begin
for (f, i) (
(identity, [3, 7]),
(Base.Fix1(getindex, [3, 7]), 1:2),
(Base.Fix1(getindex, Dict("a" => 3, "b" => 7)), ["a", "b"]),
(Base.Fix1(getfield, (; a = 3, b = 7)), 1:2),
(Base.Fix1(getfield, (; a = 3, b = 7)), [:a, :b]),
)
Expand Down

0 comments on commit 39fed7d

Please sign in to comment.