Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference function of outer module

Tags:

julia

I work on a numerical code solving two closely related problems. The following seems a good code structure to me:

module Problems

common_function(i) = println("Solving problem ", i)

module Problem1
solve() = common_function(1)
end # module Problem1

module Problem2
solve() = common_function(2)
end # module Problem2

end # module Problems

Unfortunately, it doesn't work: running Problems.Problem1.solve() results in ERROR: common_function not defined. Can this be fixed?

Remark: I have more than just one function per module such that replacing the Problem1 module with a problem1_solve() function would not be a very nice solution.

like image 377
gTcV Avatar asked Sep 11 '26 11:09

gTcV


1 Answers

In version 1.0, you have to import from the relative path to the parent:

module Problems
common_function(i) = println("Solving problem ", i)

module Problem1
    import ..Problems: common_function
    solve() = common_function(1)
end # module Problem1

end # module Problems

This also works with using ..Problems.

like image 70
phipsgabler Avatar answered Sep 13 '26 06:09

phipsgabler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!