Skip to content

Commit 162b9e9

Browse files
authored
more consistent findall output (#45538)
fix #45495
1 parent 407dbdb commit 162b9e9

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

base/array.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -2352,7 +2352,11 @@ julia> findall(x -> x >= 0, d)
23522352
23532353
```
23542354
"""
2355-
findall(testf::Function, A) = collect(first(p) for p in pairs(A) if testf(last(p)))
2355+
function findall(testf::Function, A)
2356+
T = eltype(keys(A))
2357+
gen = (first(p) for p in pairs(A) if testf(last(p)))
2358+
isconcretetype(T) ? collect(T, gen) : collect(gen)
2359+
end
23562360

23572361
# Broadcasting is much faster for small testf, and computing
23582362
# integer indices from logical index using findall has a negligible cost

test/functional.jl

+7
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ end
139139
@test findall(!iszero, x^2 for x in -1:0.5:1) == [1, 2, 4, 5]
140140
@test argmin(x^2 for x in -1:0.5:1) == 3
141141

142+
# findall return type, see #45495
143+
let gen = (i for i in 1:3);
144+
@test @inferred(findall(x -> true, gen))::Vector{Int} == [1, 2, 3]
145+
@test @inferred(findall(x -> false, gen))::Vector{Int} == Int[]
146+
@test @inferred(findall(x -> x < 0, gen))::Vector{Int} == Int[]
147+
end
148+
142149
# inference on vararg generator of a type (see #22907 comments)
143150
let f(x) = collect(Base.Generator(=>, x, x))
144151
@test @inferred(f((1,2))) == [1=>1, 2=>2]

0 commit comments

Comments
 (0)