# (Re-)exporting from extension

**URL:** https://discourse.julialang.org/t/re-exporting-from-extension/104139
**Category:** General Usage
**Tags:** pkg, package-extensions
**Created:** [September 22, 2023, 9:53am UTC](https://discourse.julialang.org/t/re-exporting-from-extension/104139 "2023-09-22T09:53:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![manuelbb-upb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manuelbb-upb/32/25222_2.png) [@manuelbb-upb](https://discourse.julialang.org/u/manuelbb-upb)
#### Post date: [September 22, 2023, 9:53am UTC](https://discourse.julialang.org/t/re-exporting-from-extension/104139/1 "2023-09-22T09:53:37Z")

</div>

Hi everyone,

is it possible to re-export names defined in a package extension from within the main package itself?  
For example, the hypothetical main package “Optimizer” defines an optimization structure allowing for gradient based optimization, but the user would have to provide derivatives by hand. I could now think of offering optional ForwardDiff support via Requires.jl or the new extension system.  
With the Requires approach, I was able to load the sub-module like so:

```julia
function __init__ ()
  require ForwardDiff="f6369f11-7733-5829-9624-2563aa707210" begin
    include("../ext/ForwardDiffBackendExt.jl")
    using .ForwardDiffBackendExt: ForwardDiffBackend
    export ForwardDiffBackend
  end
end

```

Now, if ForwardDiff has been loaded, a user could conveniently use `ForwardDiffBackend`.  
Can I achieve something similar with extensions? That is, load the extension in the main package and maybe even export names from it?  
I tried

```julia
function __init__ ()
     m = Base.get_extension(@ __MODULE__ , :ForwardDiffBackendExt)
     global ForwardDiffBackend = isnothing(m) ? m : m.ForwardDiffBackend
 end

```

But this gives

```julia
ERROR: LoadError: InitError: UndefVarError: `ForwardDiffBackend` not defined

```

and

```julia
Error: Error during loading of extension ForwardDiffBackendExt of Optimizer, use `Base.retry_load_extensions()` to retry.

```

This issue seems related:

> <https://github.com/JuliaLang/julia/issues/49990>
>
> Right now when a user wants to use an extension, they load the dependency themse…lves, and this triggers the extension. However, this requires the user to know when for what part of the code need to use it.
> 
> I think it would be very useful for downstream users of a package if there was a way for a package maintainer to automatically trigger a particular extension from \_within\_ their package.
> 
> For example, say I want to use Zygote.jl inside my package, but only for some rarely-used branch of my code. Due to the 300ms load time, I don't want to always load it. I could set up an extension, but it would require all downstream users to load Zygote.jl manually. But downstream users might not even know where Zygote is being used inside the package, or if it is being used at all.
> 
> So, it would be great if there could be a way to manually trigger an extension to load. Something like \`Base.load\_extension(@\_\_MODULE\_\_, :ZygoteExt)\`, similar to \`get\_extension\`.
> 
> @KristofferC wdyt?
> 
> Related discussion: https://github.com/johnnychen94/LazyModules.jl/issues/10
> 
> \---
> 
> \*\*Edit\*\*: here's a clarifying post: https://github.com/JuliaLang/julia/issues/49990#issuecomment-1571195689

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [September 22, 2023, 10:11am UTC](https://discourse.julialang.org/t/re-exporting-from-extension/104139/2 "2023-09-22T10:11:08Z")

</div>

Related docs PR, if anyone wants to improve / merge it:

> <https://github.com/JuliaLang/Pkg.jl/pull/3552>
>
> Partial fix for #3499 
> 
> It's a very minimal addition and I would welcome more …contributions

---

<div class="post-metadata">

### Author: ![manuelbb-upb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manuelbb-upb/32/25222_2.png) [@manuelbb-upb](https://discourse.julialang.org/u/manuelbb-upb)
#### Post date: [September 22, 2023, 1:28pm UTC](https://discourse.julialang.org/t/re-exporting-from-extension/104139/3 "2023-09-22T13:28:44Z")

</div>

Thanks! From looking at it, what I have in mind is not supported/recommended as of yet (?)  
Current workaround is a getter-function that I export instead.

---

<div class="post-metadata">

### Author: ![kongdd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kongdd/32/17058_2.png) [@kongdd](https://discourse.julialang.org/u/kongdd)
#### Post date: [July 11, 2024, 11:24am UTC](https://discourse.julialang.org/t/re-exporting-from-extension/104139/4 "2024-07-11T11:24:13Z")

</div>

I also need this feature. Just wondering whether there is a solution?
