Julia Type Traits - prl-julia/julia-type-stability GitHub Wiki

Often times you need to analyze a Julia type (like Int64 or Vector{T} where T). There's a bunch of properties you may be interested in, for which Julia supplies a number of query functions. Julia docs have a useful section on some of those functions (whereas some other are completely undocumented):

Some of more useful ones.

  • Abstractness / concreteness: isabstracttype / isconcretetype.

  • Struct-types: isstructtype.

    • Singleton types: issingletontype.

      Determine whether type T has exactly one possible instance; for example, a struct type with no fields.

    • Field names: fieldnames (luckily, works on types, not instances).

  • Primitive / bitstypes: isprimitivetype / isbitstype.

  • Mutability -- only on instances: ismutable.

  • isdispatchtuple: tuples are weird in some respects making the other tools fail to apply, hence this special-cased utility.

    Determine whether type T is a tuple "leaf type", meaning it could appear as a type signature in dispatch and has no subtypes (or supertypes) which could appear in a call.

  • Union-All: typeof(yourtype) == UnionAll.

  • Varargs and variable-size tuples: isvarargtype, isvatuple (Base).

When build-in functions won't cut it, resort to dump, e.g. to extract bounds of type variables in an existential:

julia> struct MyIntVec{T<:Integer}; data :: Vector{T}; end

julia> dump(MyIntVec)
UnionAll
  var: TypeVar
    name: Symbol T
    lb: Union{}
    ub: Integer <: Real
  body: MyIntVec{T<:Integer} <: Any
    data::Vector{T}

Type instantiation: curiously if a variable stores a UnionAll, you can apply a type to it via the usual {}-notation.

Subtype Hierarchy

  • supertype(s)

  • subtypes (for some reason, in the InteractiveUtils, not Base)

Manipulation

  • Base.unwrap_unionall — a rather low-level tool, not documented. Removes the where part from a union-all leaving a type be with free type variables. This sounds strange but this form allows some sorts of syntactic type analysis proceed.