Does Julia have function prototypes?

question

#1

Hello. I am wondering if it’s possible to have function prototypes, similar to what one does in C or C++ in a header file.

I would like to have such prototypes because it makes it very easy to read a julia file and see the functions it provides, without having to grep through hundreds of lines of code to see which functions and types are defined there. So far, from what I’ve seen, there are no function prototypes. If this is the case, do you have any advice on how to achieve achieve something of this sort, that would help have a quick understanding of the functions provided by a module or julia file?

Thanks!


#2

Just look at module’s export statement.

Also, you can list all exported functions and types from a module using names(ModuleName) or all external + internal using names(ModuleName, true):

julia> import IterTools
INFO: Precompiling module IterTools.

julia> names(IterTools)
18-element Array{Symbol,1}:
 Symbol("@itr")
 :IterTools    
 :chain        
 :count        
 :distinct     
 :groupby      
 :imap         
 :iterate      
 :ncycle       
 :nth          
 :partition    
 :peek         
 :peekiter     
 :product      
 :repeatedly   
 :subsets      
 :takenth      
 :takestrict

#3

You could look at https://github.com/yuyichao/FunctionWrappers.jl. That package is mostly to handle the case where you want to avoid the dynamic dispatch when you have e.g. a vector of heterogeneous function with the same prototype but you could perhaps use it here.

There are some optimizations (like inlining) that will not work when it is wrapped though so it is not an ideal solution.


#4

Thanks for the answers. It seems that for my case the best will be to have proper docstrings and generate the documentation per file or module, so it’s easy to know where each function is and depart from there.

Looking at the exports can help, but I was looking for something that could be seen directly in the source file, similar to how one looks at the .h file to see what a class is implementing, without having to grep into the respective .cpp file in C++; i find that very handy.


#5

What information would you expect from a “function prototype” in Julia? Since methods can have different number of arguments, behavior, and returned types, pretty much the only information that is common to methods of a function is the function name.


#6

I agree Tamas that it’s tricky because of the variable number of arguments and templated functions. My motivation was mostly to have a quick summary of what a .jl file contains, even if a bit incomplete.

What I’m trying now is to use Documenter.jl to build the relevant docs so it’s easier to navigate. Thanks!


#7

The problem is exacerbated by the fact that the methods may be scattered over multiple .jl files. While I like to put everything in a single file below 500 LOC, some Julia programmers like to keep things more separated. For some well-organized packages, you may find a file that defines the types and possibly the generic function stubs with syntax like

"""
here foo is documented nicely, methods will be all over the place
"""
function foo end

Then you can work your way through the other files which have the methods.

I usually call the @edit macro to take me to the relevant method. When the debugger stabilizes, it will become even easier to explore code in a package.

Quick text searches also help — I find ag very useful for that.