Correct use of Requires.jl


#1

Hello,
I’ve been trying to use the Requires.jl package to lazy load plotting functions and other things. I tried the following example, and I get a not defined error.

module Req

using Requires

export myfun

@require DataFrames begin
myfun(n::Int64) = n+1
end

end
julia> using Req

julia> myfun(5)
ERROR: UndefVarError: myfun not defined

What is the proper use. Thank you


#2

Only after you load DataFrames is myfun defined. I think that’s the intended behaviour:

julia> using DataFrames

julia> myfun(5)
6

#3

But that defeats the purpose of avoid loading DataFrames. I would expect the using DataFrames to happen automatically when myfun is called for the first time


#4

That’s not what Requires is for. Requires is to make code available only after a given package is loaded.


#5

I see. I was understanding it wrong. BTW, is there a way to accomplish the behavior I want?.
Thank you!


#6

What do you mean by “lazy loading”? The costly phase is the AOT compilation, which happens (by default) when the function is used, not before. If you use __precompile__(), you can speed up the load time a bit.