Julia provides a variety of runtime reflection capabilities.
The public names for aModule
are available usingnames(m::Module)
, which will return an array ofSymbol
elements representing the public bindings.names(m::Module, all = true)
returns symbols for all bindings inm
, regardless of public status.
The names ofDataType
fields may be interrogated usingfieldnames
. For example, given the following type,fieldnames(Point)
returns a tuple ofSymbol
s representing the field names:
julia> struct Point x::Int y endjulia> fieldnames(Point)(:x, :y)
The type of each field in aPoint
object is stored in thetypes
field of thePoint
variable itself:
julia> Point.typessvec(Int64, Any)
Whilex
is annotated as anInt
,y
was unannotated in the type definition, thereforey
defaults to theAny
type.
Types are themselves represented as a structure calledDataType
:
julia> typeof(Point)DataType
Note thatfieldnames(DataType)
gives the names for each field ofDataType
itself, and one of these fields is thetypes
field observed in the example above.
Thedirect subtypes of anyDataType
may be listed usingsubtypes
. For example, the abstractDataType
AbstractFloat
has four (concrete) subtypes:
julia> InteractiveUtils.subtypes(AbstractFloat)5-element Vector{Any}: BigFloat Core.BFloat16 Float16 Float32 Float64
Any abstract subtype will also be included in this list, but further subtypes thereof will not; recursive application ofsubtypes
may be used to inspect the full type tree.
Note thatsubtypes
is located insideInteractiveUtils
but is automatically exported when using the REPL.
The internal representation of aDataType
is critically important when interfacing with C code and several functions are available to inspect these details.isbitstype(T::DataType)
returns true ifT
is stored with C-compatible alignment.fieldoffset(T::DataType, i::Integer)
returns the (byte) offset for fieldi relative to the start of the type.
The methods of any generic function may be listed usingmethods
. The method dispatch table may be searched for methods accepting a given type usingmethodswith
.
As discussed in theMetaprogramming section, themacroexpand
function gives the unquoted and interpolated expression (Expr
) form for a given macro. To usemacroexpand
,quote
the expression block itself (otherwise, the macro will be evaluated and the result will be passed instead!). For example:
julia> InteractiveUtils.macroexpand(@__MODULE__, :(@edit println("")) ):(InteractiveUtils.edit(println, (Base.typesof)("")))
The functionsBase.Meta.show_sexpr
anddump
are used to display S-expr style views and depth-nested detail views for any expression.
Finally, theMeta.lower
function gives thelowered
form of any expression and is of particular interest for understanding how language constructs map to primitive operations such as assignments, branches, and calls:
julia> Meta.lower(@__MODULE__, :( [1+2, sin(0.5)] )):($(Expr(:thunk, CodeInfo( @ none within `top-level scope`1 ─ %1 = 1 + 2│ %2 = sin(0.5)│ %3 = Base.vect(%1, %2)└── return %3))))
Inspecting the lowered form for functions requires selection of the specific method to display, because generic functions may have many methods with different type signatures. For this purpose, method-specific code-lowering is available usingcode_lowered
, and the type-inferred form is available usingcode_typed
.code_warntype
adds highlighting to the output ofcode_typed
.
Closer to the machine, the LLVM intermediate representation of a function may be printed using bycode_llvm
, and finally the compiled machine code is available usingcode_native
(this will trigger JIT compilation/code generation for any function which has not previously been called).
For convenience, there are macro versions of the above functions which take standard function calls and expand argument types automatically:
julia> @code_llvm +(1,1); @ int.jl:87 within `+`; Function Attrs: sspstrong uwtabledefine i64 @"julia_+_476"(i64 signext %0, i64 signext %1) #0 {top: %2 = add i64 %1, %0 ret i64 %2}
For more information see@code_lowered
,@code_typed
,@code_warntype
,@code_llvm
, and@code_native
.
The aforementioned functions and macros take the keyword argumentdebuginfo
that controls the level debug information printed.
julia> InteractiveUtils.@code_typed debuginfo=:source +(1,1)CodeInfo( @ int.jl:87 within `+`1 ─ %1 = Base.add_int(x, y)::Int64└── return %1) => Int64
Possible values fordebuginfo
are::none
,:source
, and:default
. Per default debug information is not printed, but that can be changed by settingBase.IRShow.default_debuginfo[] = :source
.
Settings
This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.