Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print in REPL the code of functions in Julia?

In Julia, a lot of the Base and closer related functions are also written in pure Julia, and the code is easily avaible. One can skim through the repository or the local downloaded files, and see how the function is written/implemented. But I think there is allready some built in method that does that for you, so you can write in REPL or Jupyter Notebook something like:

@code functioninquestion()

and get something like:

functioninquestion(input::Type) some calculations return end without paging throug the code. I just don't remember the method or call. I have read the Reflection/Introspection section of the Manual but I cannot seem to be able to use anything there. I've tried methods, methodswith, code_lowered, expand and cannot seem to make them give what I want-

like image 972
wpkzz Avatar asked Jun 06 '16 16:06

wpkzz


People also ask

How do I print a function in Julia?

The most common function to print the output of the program in the console of Julia is print() and println(). To execute this command we just need to press Enter on the keyboard. The main difference is that the println() function adds a new line to the end of the output.

How do I run Julia code in REPL?

Enter the interactive mode of Julia, by inputting the command julia on the command line. You will now be in the Julia "REPL" (read-eval-print loop) with the prompt julia> on the left of your screen. Note that you must be in the same folder as code. jl for this to work; otherwise input the entire path to the code.


2 Answers

This is not currently supported but probably will be in the future.

like image 109
StefanKarpinski Avatar answered Oct 12 '22 08:10

StefanKarpinski


Though this may not be what the OP is looking for, @less is very convenient to read the underlying code (so I very often use it). For example,

julia> @less 1 + 2

gives

+(x::Int, y::Int) = box(Int,add_int(unbox(Int,x),unbox(Int,y)))

which corresponds to the line given by

julia> @which 1 + 2
+(x::Int64, y::Int64) at int.jl:8
like image 20
roygvib Avatar answered Oct 12 '22 08:10

roygvib