Skip to content

Commit 7b83a36

Browse files
committed
Preserve the input element type in unique
Previously the element type of the output was the smallest type that would fit the union of the input's individual element types. Now the output has an identical element type to the input. Fixes #22696.
1 parent 086eca1 commit 7b83a36

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ This section lists changes that do not have deprecation warnings.
150150
* Worker-worker connections are setup lazily for an `:all_to_all` topology. Use keyword
151151
arg `lazy=false` to force all connections to be setup during a `addprocs` call. ([#22814])
152152

153+
* The element type of the input is now preserved in `unique`. Previously the element type
154+
of the output was shrunk to fit the union of the type of each element in the input.
155+
([#22696])
156+
153157
Library improvements
154158
--------------------
155159

@@ -1172,6 +1176,7 @@ Command-line option changes
11721176
[#22588]: https://github.com/JuliaLang/julia/issues/22588
11731177
[#22605]: https://github.com/JuliaLang/julia/issues/22605
11741178
[#22666]: https://github.com/JuliaLang/julia/issues/22666
1179+
[#22696]: https://github.com/JuliaLang/julia/issues/22696
11751180
[#22703]: https://github.com/JuliaLang/julia/issues/22703
11761181
[#22712]: https://github.com/JuliaLang/julia/issues/22712
11771182
[#22718]: https://github.com/JuliaLang/julia/issues/22718

base/set.jl

+8-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ const ⊆ = issubset
240240
241241
Return an array containing only the unique elements of collection `itr`,
242242
as determined by [`isequal`](@ref), in the order that the first of each
243-
set of equivalent elements originally appears.
243+
set of equivalent elements originally appears. The element type of the
244+
input is preserved.
244245
245246
# Examples
246247
```jldoctest
@@ -249,6 +250,11 @@ julia> unique([1, 2, 6, 2])
249250
1
250251
2
251252
6
253+
254+
julia> unique(Real[1, 1.0, 2])
255+
2-element Array{Real,1}:
256+
1
257+
2
252258
```
253259
"""
254260
function unique(itr)
@@ -260,7 +266,7 @@ function unique(itr)
260266
return out
261267
end
262268
x, i = next(itr, i)
263-
if !isleaftype(T)
269+
if !isleaftype(T) && iteratoreltype(itr) == EltypeUnknown()
264270
S = typeof(x)
265271
return _unique_from(itr, S[x], Set{S}((x,)), i)
266272
end

test/sets.jl

+2
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ u = unique([1,1,2])
233233
# issue 20105
234234
@test @inferred(unique(x for x in 1:1)) == [1]
235235
@test unique(x for x in Any[1,1.0])::Vector{Real} == [1]
236+
@test unique(x for x in Real[1,1.0])::Vector{Real} == [1]
237+
@test unique(Integer[1,1,2])::Vector{Integer} == [1,2]
236238

237239
# unique!
238240
@testset "unique!" begin

0 commit comments

Comments
 (0)