# Correct use of Requires.jl

**URL:** <https://discourse.julialang.org/t/correct-use-of-requires-jl/10493>\
**Category:** New to Julia\
**Created:** [April 23, 2018, 7:08pm UTC](https://discourse.julialang.org/t/correct-use-of-requires-jl/10493 "2018-04-23T19:08:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [April 23, 2018, 7:08pm UTC](https://discourse.julialang.org/t/correct-use-of-requires-jl/10493/1 "2018-04-23T19:08:36Z")

</div>

Hello,  
I’ve been trying to use the [Requires.jl](https://github.com/MikeInnes/Requires.jl) package to lazy load plotting functions and other things. I tried the following example, and I get a `not defined` error.

```julia
module Req

using Requires

export myfun

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

end

```

```julia
julia> using Req

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

```

What is the proper use. Thank you

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [April 23, 2018, 7:13pm UTC](https://discourse.julialang.org/t/correct-use-of-requires-jl/10493/2 "2018-04-23T19:13:41Z")

</div>

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

```julia
julia> using DataFrames

julia> myfun(5)
6

```

---

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [April 23, 2018, 7:31pm UTC](https://discourse.julialang.org/t/correct-use-of-requires-jl/10493/3 "2018-04-23T19:31:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 23, 2018, 7:39pm UTC](https://discourse.julialang.org/t/correct-use-of-requires-jl/10493/4 "2018-04-23T19:39:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [April 23, 2018, 7:43pm UTC](https://discourse.julialang.org/t/correct-use-of-requires-jl/10493/5 "2018-04-23T19:43:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 24, 2018, 1:44am UTC](https://discourse.julialang.org/t/correct-use-of-requires-jl/10493/6 "2018-04-24T01:44:23Z")

</div>

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.
