diff --git a/base/set.jl b/base/set.jl index e6831bfaf4fe6..df98cb43174ab 100644 --- a/base/set.jl +++ b/base/set.jl @@ -133,19 +133,28 @@ function unique(itr) end x, i = next(itr, i) if !isleaftype(T) - T = typeof(x) - out = Vector{T}() - seen = Set{T}() + S = typeof(x) + return _unique_from(itr, S[x], Set{S}((x,)), i) end push!(seen, x) push!(out, x) + return unique_from(itr, out, seen, i) +end + +_unique_from(itr, out, seen, i) = unique_from(itr, out, seen, i) +@inline function unique_from{T}(itr, out::Vector{T}, seen, i) while !done(itr, i) x, i = next(itr, i) S = typeof(x) if !(S === T || S <: T) - T = typejoin(S, T) - seen = convert(Set{T}, seen) - out = convert(Vector{T}, out) + R = typejoin(S, T) + seenR = convert(Set{R}, seen) + outR = convert(Vector{R}, out) + if !in(x, seenR) + push!(seenR, x) + push!(outR, x) + end + return _unique_from(itr, outR, seenR, i) end if !in(x, seen) push!(seen, x)