Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

How to avoid repeatedly calling include()?

$
0
0

Hello Julia Community :slightly_smiling_face:,

I’m developing my first Julia package at the moment. In the main module of this package, there are many functions that call functions from other modules. For example:

module main_module

function foo(; kwargs)
   include("Module_One.jl")
   x = Module_One.Function_One(kwargs...)
   return x
end

function bar(; kwargs)
   include("Module_Two.jl")
   y = Module_Two.Function_Two(kwargs...)
   return y
end

function baz(; kwargs...)
   include("Module_Three.jl")
   z = Module_Three.Function_Three(kwargs...)
   return z
end

Unless I’m mistaken :eyes:, if I call one of these functions regularly, e.g. foo(), then each time it will look to include Module_One, which seems wasteful…

To be more efficient, would it be wise/foolish to do the following instead? :face_with_raised_eyebrow:

module main_module

function foo(; kwargs)
   (@isdefined Module_One) ? nothing : include("Module_One.jl")
   x = Module_One.Function_One(kwargs...)
   return x
end

The idea being to skip re-including “Module_One” each time the function is called again…
Or is Julia efficient enough already that doing this is unnecessary?

Having tried to run code this way already, I tend to get a world age error the first time (i.e. The applicable method may be too new: running in world age 12345 ) but it works ok after the first run…

Any advice will be greatly appreciated :pray:t3: :innocent:
Thanks!

12 posts - 8 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles