code_lowered

code_lowered(f, types)

Returns an array of lowered ASTs for the methods matching the given generic function and type signature.

Examples

julia> function add_numbers(x, y)
           return x + y
       end

julia> methods_add_numbers = code_lowered(add_numbers, Tuple{Int, Int})
2-element Vector{Any}:
 $(Expr(:lambda, Any[:x, :y], Any[], Any[:(x + y)]))
 $(Expr(:lambda, Any[:x, :y], Any[], Any[:(x + y)]))

julia> subtract_numbers(x, y) = x - y
subtract_numbers (generic function with 1 method)

julia> methods_subtract_numbers = code_lowered(subtract_numbers, Tuple{Int, Int})
1-element Vector{Any}:
 $(Expr(:lambda, Any[:x, :y], Any[], Any[:(x - y)]))

In the above examples, code_lowered is used to retrieve the lowered ASTs (Abstract Syntax Trees) for the methods matching the given generic function and type signature.

  1. Retrieve lowered ASTs for a function with specific argument types:

    julia> function add_numbers(x, y)
              return x + y
          end
    
    julia> methods_add_numbers = code_lowered(add_numbers, Tuple{Int, Int})

    The code_lowered function returns an array of lowered ASTs for the methods matching the generic function add_numbers and the given argument types Tuple{Int, Int}. In this example, there are two methods matching the signature, and their corresponding lowered ASTs are stored in the methods_add_numbers array.

  2. Retrieve lowered ASTs for a function with multiple methods:

    julia> subtract_numbers(x, y) = x - y
    subtract_numbers (generic function with 1 method)
    
    julia> methods_subtract_numbers = code_lowered(subtract_numbers, Tuple{Int, Int})

    The code_lowered function can also be used to retrieve the lowered AST for a function with multiple methods. In this example, the function subtract_numbers has one method, and its lowered AST is stored in the methods_subtract_numbers array.

Note: The returned lowered ASTs can be further analyzed or manipulated using Julia's metaprogramming capabilities.

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: