Correct use of Requires.jl

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

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

julia> using DataFrames

julia> myfun(5)
6

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

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

1 Like

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

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.