Any way to get a list of functions defined in a module?

Is there any introspective magic that will give me a list of functions defined in the module?

module Foo function foo() "foo" end function bar() "bar" end end 

Some mythical features like:

 functions_in(Foo) 

To return: [foo, bar]

+5
source share
1 answer

The problem is that both names and whos list the exported names from the module. If you want to see them, you will need to do something like this:

 module Foo export foo, bar function foo() "foo" end function bar() "bar" end end # module 

At this point, all names and whos will list everything.

If you work in REPL and for some reason do not want to export any names, you can check the contents of the module interactively by typing Foo.[TAB] . See an example from this session:

 julia> module Foo function foo() "foo" end function bar() "bar" end end julia> using Foo julia> whos(Foo) Foo Module julia> names(Foo) 1-element Array{Symbol,1}: :Foo julia> Foo. bar eval foo 

Somehow tab completion looks through non-exportable names, so there should be a way to get Julia to tell them about you. I just don’t know what this function is.

EDIT


I ruined a little. The exported Base.REPLCompletions.completions function Base.REPLCompletions.completions not work, as shown in the continuation of the previous REPL session:

 julia> function functions_in(m::Module) s = string(m) out = Base.REPLCompletions.completions(s * ".", length(s)+1) # every module has a function named `eval` that is not defined by # the user. Let filter that out return filter(x-> x != "eval", out[1]) end functions_in (generic function with 1 method) julia> whos(Foo) Foo Module julia> names(Foo) 1-element Array{Symbol,1}: :Foo julia> functions_in(Foo) 2-element Array{UTF8String,1}: "bar" "foo" 
+7
source

Source: https://habr.com/ru/post/1214201/


All Articles