@isdefined in eltype implementation


I’m trying to figure out what @isdefined is doing here:

eltype(::Type{<:AbstractArray{E}}) where {E} = @isdefined(E) ? E : Any

How can E not be defined at this point?


1 Like

OK, here is an example

import Base.eltype
eltype(::Type{<:AbstractArray{E}}) where {E} = @isdefined(E) ? E : @assert false

@show eltype(Vector{Number}) 
@show eltype(Vector{Unknown}) where Unknown

Pretty sophisticated check there…

1 Like

In general methods can get called and not all type parameters resolve to something. A more clear example might be:

foo(args::T...) where {T} = @isdefined(T)
foo() # false

I can’t think of a great example for eltype there, but e.g. eltype(Base.unwrap_unionall(Vector)) is hitting that branch. Corner case no doubt, but you wouldn’t want that to error otherwise.