Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map with an invalid function body returns Union{}[] #47415

Closed
BioTurboNick opened this issue Nov 1, 2022 · 3 comments
Closed

map with an invalid function body returns Union{}[] #47415

BioTurboNick opened this issue Nov 1, 2022 · 3 comments

Comments

@BioTurboNick
Copy link
Contributor

In 1.8.2:

using NearestNeighbors
map([]) do f
	knn(BallTree(), [])
end # Union{}[]

This inner method call is invalid and will never work for non-empty input, but for consistency, shouldn't the return type for the empty case either be Nothing[] or Any[]?

map([]) do f
end # Nothing[]

map([]) do f
    knn(BallTree([1.0; 2.0]), [1.0; 2.0], 1)
end # Any[]
@BioTurboNick BioTurboNick changed the title map with an invalid contents returns Union{}[] map with an invalid function body returns Union{}[] Nov 1, 2022
@mcabbott
Copy link
Contributor

mcabbott commented Nov 1, 2022

This is the smallest type, while Any is the largest. I think that ensures it can be composed with anything, like so:

julia> map(_ -> error(), [])
Union{}[]

julia> collect(Iterators.flatten(()))
Union{}[]

julia> vcat(ans, [true])  # 1st arg never widens the type
1-element Vector{Bool}:
 1

julia> vcat(Nothing[], [true])
1-element Vector{Union{Nothing, Bool}}:
 true

@vtjnash vtjnash closed this as completed Nov 3, 2022
@vtjnash
Copy link
Sponsor Member

vtjnash commented Nov 3, 2022

Closing as not planned, since I think this is per-design. But feel free to make an argument why it is not right however!

@BioTurboNick
Copy link
Contributor Author

Thanks! I don't know if I have one, other than it being unusual.

For context, the reason I noticed was that in Pluto (via Colors), when it tried to show the object, it encountered a type ambiguity which to me seemed nonintuitive:

ERROR: MethodError: show(::Base.TTY, ::MIME{Symbol("image/svg+xml")}, ::Vector{Union{}}) is ambiguous. Candidates:
  show(io::IO, mime::MIME{Symbol("image/svg+xml")}, cs::AbstractVector{T}; max_swatches) where T<:TransparentColor in Colors at /Users/fons/.julia/packages/Colors/yDxFN/src/display.jl:41
  show(io::IO, mime::MIME{Symbol("image/svg+xml")}, cs::AbstractVector{T}; max_swatches) where T<:Color in Colors at /Users/fons/.julia/packages/Colors/yDxFN/src/display.jl:35
Possible fix, define
  show(::IO, ::MIME{Symbol("image/svg+xml")}, ::AbstractVector{Union{}})

JuliaGraphics/Colors.jl#526

Should methods need to guard against Union{} inputs causing ambiguity?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants