code_typed

code_typed(f, types; optimize=true)

Returns an array of lowered and type-inferred ASTs for the methods matching the given generic function and type signature. The keyword argument optimize controls whether additional optimizations, such as inlining, are also applied.

Examples

julia> function add_numbers(a::Int, b::Int)
           return a + b
       end

julia> code_typed(add_numbers, (Int, Int))
1-element Array{Any,1}:
 CodeInfo(
1 ─ %1 = (Base.add_int)(a, b)::Int64
└──      return %1
)

In the above example, we define a function add_numbers that takes two Int arguments and returns their sum. We then use code_typed to get the lowered and type-inferred AST for the method matching the given generic function and type signature (Int, Int). The result is an array containing a single CodeInfo object representing the typed code.

By default, code_typed applies additional optimizations, such as inlining, if the optimize keyword argument is set to true. Here's an example with optimization disabled:

julia> code_typed(add_numbers, (Int, Int); optimize=false)
1-element Array{Any,1}:
 CodeInfo(
1 ─ %1 = (Base.add_int)(a, b)::Int64
└──      return %1
)

It returns the same result as before because the function is simple and doesn't benefit from additional optimizations.

Please note that the examples provided here are just for illustration purposes and may vary depending on the Julia version and compiler optimizations.

See Also

assert, backtrace, code_llvm, code_lowered, code_native, code_typed, code_warntype, :@which, compilecache, current_module, eval, finalize, finalizer, fullname, function_module, function_name, include_dependency, InterruptException, invoke, isconst, isdefined, isgeneric, methodswith, method_exists, module_name, module_parent, require, subtypes, unsafe_load, workspace, __precompile__,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: