When working with Julia, it is often necessary to inspect the contents of a function. This can be useful for understanding how the function works, debugging, or simply exploring the code. In this article, we will explore three different ways to see what’s inside a function in Julia.
Using the @edit macro
One way to see what’s inside a function is to use the @edit macro. This macro allows you to open the source code of a function in your default editor. To use it, simply prepend the function call with @edit. For example:
@edit my_function()
This will open the source code of the my_function() function in your default editor, allowing you to see the implementation details.
Using the @which macro
Another way to see what’s inside a function is to use the @which macro. This macro allows you to see the location of the method definition for a given function. To use it, simply prepend the function call with @which. For example:
@which my_function()
This will display the location of the method definition for the my_function() function. This can be useful for understanding which file or module the function is defined in.
Using the @code_lowered macro
The @code_lowered macro allows you to see the lowered representation of a function. This can be useful for understanding how the function is transformed before being executed. To use it, simply prepend the function call with @code_lowered. For example:
@code_lowered my_function()
This will display the lowered representation of the my_function() function. This can provide insights into how the function is optimized and executed by the Julia compiler.
Of the three options, the best one depends on your specific use case. If you want to see the source code of a function, the @edit macro is the most suitable. If you are interested in the method definition location, the @which macro is the way to go. And if you want to understand the lowered representation of a function, the @code_lowered macro is the most appropriate.