Is there an isabstract(T) function where T is a DataType?


#1

I wonder if there is a function that returns whether a type T is abstract or not. Any idea? I am trying to make a generator that returns all concrete types of a specific abstract type, and I need to check whether a certain type is abstract or not.

subtypes(T) is no good because it cannot differentiate between abstract types with no subtype (useful to have around for future support plans) and concrete types.


#2

Base.isabstract


#3
julia> isabstract
ERROR: UndefVarError: isabstract not defined

#4

It’s not exported, which means that you have to explicitly do Base.isabstract as opposed to isabstract.


#5
julia> Base.isabstract
ERROR: UndefVarError: isabstract not defined

#6

Hmm… what version of Julia are you using? You should upgrade to the latest 0.6 release candidate.


#7

I am using 0.5. Ok if I have to upgrade, then I will just manually enumerate them instead!


#8

It’s in Compat


#9
julia> using Compat

julia> isabstract
ERROR: UndefVarError: isabstract not defined

julia> Compat.isabstract
ERROR: UndefVarError: isabstract not defined

#10

#11

Ic. Thanks alot!

julia> Compat.TypeUtils.isabstract
isabstract (generic function with 3 methods)

julia> Compat.TypeUtils.isabstract(Int)
false

julia> Compat.TypeUtils.isabstract(Integer)
true

#12

I was just searching for an isabstract function and also found Base.isabstract. However, I noticed Base.isabstract(Complex) == false which seems wrong to me.

But someone on Slack pointed out that there is isleaftype. I thought it might be useful to mention it here.


#13

From v0.7:

julia> isleaftype(Array)
┌ Warning: `isleaftype` is deprecated, use `isconcrete` instead.
│   caller = top-level scope
└ @ Core :0
false

#14

Even better!


#15

why? it is not an abstract type.


#16

It is a UnionAll type, which is NOT a concrete type. But I don’t know if not being concrete is the definition of an abstract type :thinking:


#17

Abstract type = abstract type


#18

But it isn’t. See the documentation for isconcrete:

Determine whether T is a concrete type, meaning it can have direct instances (values x such that typeof(x) === T).

I agree that the terminology is not the most intuitive. Similarly to topology, where a set not being open does not mean it is closed.