Access variable inside of a funciton from another function

hello, everyone, I have a question about how to access local variable of a funciton in another funciton, my code is like this:

function FA()
    name = "world"
    return "hello"
end
function FB(f::Function)
    println(f.name)
end

FB(FA)

get this error:

ERROR: type #FA has no field name

so how to get variable name of FA in FB ?

thanks for any help~
Si

The short answer is: you can’t. But perhaps if you can explain why you want to access that information we can help you find another way.

2 Likes

thanks~
I am trying to implement some basic features of python-fire package(https://github.com/google/python-fire) with julia. My current work result is https://github.com/orangeSi/Jfire.jl, Jfire can call modules or functions, but still cann’t output more help information about funciton parameters.

assume you have a file myth.jl is like this:

using Jfire

module myth
export hello
function hello(;name::String="sikaiwei", greet::String="how is the weather?", number::Number=3)
	println("hello, $name. $greet. $number")
end
end

if abspath(PROGRAM_FILE) == @__FILE__
	Jfire.Fire(myth, time=true, color=:yellow, info=false)
end

so you can call help information like:

julia myth.jl hello -h

will output

Module myth
Function hello:

Keyword Arguments:(optional)
	--name
	--greet
	--number

but I want to output more detail description about --name/–greet/–number(example:

 --name some_detail_description 

),
so at first I try to defined these description in Function hello, then maybe I call access these description in Jfire.Fire.

I am curious, there is some language that you know that allows this kind of construct? I do not know any language that allow this.

You could have all variables of the function A inside a dict (or another structure) that is returned by the function A and used by function B. Or use a global variable shared by both (what is terrible for performance in Julia). Or function A is a (possibly global) object with many fields and that is callable. Really depends on what is your objective here.

1 Like

i think javascript allows this

yes, some filed that is callable is a good choice, which filed I can use ?
A is just a julia Function

I am almost sure Javascript does not allow this, unless you are referring to implicit globals.

1 Like

If I understand correctly the purpose of the Python Fire library that you are trying to replicate, it does not seem to look at variables inside other functions, but instead it uses reflection to inspect the list of parameters of a method, print them to the user, and interpret command line arguments to call some registered function and pass the arguments to it.

If you want to do more (print extra info on the parameters that is not readily available by reflexivity), maybe you could establish some convention, as that extra info is stored in a const global with the name of the function plus a suffix, and check if it is defined when you register a function. If it is, you use the information provided there to complement your output. I am not sure however, how this would work with Julia allowing multiple definitions of the same function. Python allows function overload by the method signature? How PythonFire deals with that?

Someone with more experience than me could say if this a good idea.

1 Like

@Henrique_Becker thanks~ Firstly I tried as your suggestion, it works! but I just want to write less code for display the detail parameter information. So I use the the a another way like this(by thedoc = Jfire.fire_doc(@__FILE__) # I derictly read the source path):

__precompile__()

module fasta
export base_freq, stat_fasta, read_fasta, thedoc

using Jfire 
thedoc = Jfire.fire_doc(@__FILE__)  

function base_freq(fasta::String; # input fasta file
				   prefix::String="auto", outdir::String="./",
				   chr_id::String="", # input chromosome id if only need one 
				   base::String="ATCGN",### for base
				   ignore_case::Bool=true,
				   window_size::Integer=0, # set window size bp
				   )
end

then I can get this help information:

Positional Arguments:
	fasta::String		 , input fasta file
Keyword Arguments:(optional)
	--prefix::String	default "auto" 
	--outdir::String	default "./" 
	--chr_id::String	default ""  , input chromosome id if only need one 
	--base::String	default "ATCGN"  , for base
	--ignore_case::Bool	default true 
	--window_size::Integer	default 0  , set window size bp

1 Like

Oh, I did not think of using comments close to the parameters. I was considering using the information available in the module after compiled and that could be inspected by Julia code importing the module. However, if you can call a custom parser over the file itself, well, you can do virtually anything you want.

yes, I am expecting a more elegant way, thanks~