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

Define eltype(::Type{Union{}}) = Union{}? #20140

Closed
TotalVerb opened this issue Jan 20, 2017 · 6 comments
Closed

Define eltype(::Type{Union{}}) = Union{}? #20140

TotalVerb opened this issue Jan 20, 2017 · 6 comments
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@TotalVerb
Copy link
Contributor

Currently, eltype(Union{}) throws a method ambiguity error. Since there is nothing of type Union{}, it is natural that are no objects that are elements of objects of type Union{}, so the definition eltype(::Type{Union{}}) = Union{} makes sense.

@Keno
Copy link
Member

Keno commented Jan 20, 2017

The problem you run into is that your inferred result becomes Union{Union{},Foo}, whereas an error collapses the union.

@TotalVerb
Copy link
Contributor Author

This seems to be a change from 0.5. I came across it while trying to get Lint to work on 0.6.

0.5:

julia> eltype(Union{})
Union{}

0.6:

julia> eltype(Union{})
ERROR: MethodError: eltype(::Type{Union{}}) is ambiguous. Candidates:
  eltype{E,T<:Tuple{Vararg{E,N} where N}}(::Type{T}) in Base at tuple.jl:52
  eltype{E,A<:(AbstractArray{E,N} where N)}(::Type{A}) in Base
  eltype{T<:AbstractString}(::Type{T}) in Base at strings/basic.jl:54
  eltype{T<:Base.Dates.Period}(::Type{T}) in Base.Dates at dates/types.jl:238
  eltype{T<:Number}(::Type{T}) in Base at number.jl:28

@Keno, I don't understand what you mean.

@Keno
Copy link
Member

Keno commented Jan 20, 2017

julia> Base.return_types(eltype, Tuple{(Type{T} where T<:(Array{Int64,N} where N))})
1-element Array{Any,1}:
 Type{Int64}

julia> Base.eltype(::Type{Union{}}) = Union{}

julia> Base.return_types(eltype, Tuple{(Type{T} where T<:(Array{Int64,N} where N))})
2-element Array{Any,1}:
 Type{Union{}}
 Type{Int64}

@TotalVerb
Copy link
Contributor Author

OK, so this change would pessimize type inference in certain situations, and was therefore intentional?

@ararslan ararslan added the types and dispatch Types, subtyping and method dispatch label Jan 20, 2017
@JeffBezanson
Copy link
Member

It's a koan: "if you have no object at all, is it iterable?"

I would say no.

@timholy
Copy link
Member

timholy commented Feb 11, 2017

We should get into the habit of implementing these policy decisions via informative error messages. As it is now, this is an ambiguity error which leaves the intentionality...ambiguous. See #20568.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

5 participants