# Should we define new functions/structs in an extension?

**URL:** https://discourse.julialang.org/t/should-we-define-new-functions-structs-in-an-extension/103361
**Category:** Package Management
**Tags:** package-extensions
**Created:** [August 30, 2023, 10:59am UTC](https://discourse.julialang.org/t/should-we-define-new-functions-structs-in-an-extension/103361 "2023-08-30T10:59:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)
#### Post date: [August 30, 2023, 10:59am UTC](https://discourse.julialang.org/t/should-we-define-new-functions-structs-in-an-extension/103361/1 "2023-08-30T10:59:20Z")

</div>

Newly defined functions/structs exposed in package extensions are not exposed in the main package (also not once the extension is loaded). See this related question and workaround: [How to use functions defined in package extensions but not the main package?](https://discourse.julialang.org/t/how-to-use-functions-defined-in-package-extensions-but-not-the-main-package/99979)

The workaround is nice, but doing this excessively creates a lot of boiler plate code in the main package. Additionally, for the function/struct to be exposed, I think it would need to be exported by the main package (or else some additional step would be required on the user end).

My questions is therefore: should we define new functions/structs in an extension at all?

It seems like a pretty common use case to add entirely new functionality through an extension, but maybe in most cases this just calls for an entirely separate package? What am I missing?

Thanks 🙂

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [August 30, 2023, 11:17am UTC](https://discourse.julialang.org/t/should-we-define-new-functions-structs-in-an-extension/103361/2 "2023-08-30T11:17:54Z")

</div>

I remember a few slack discussions (can check whether we had a thread here as well), where the conclusion was:

An extensions _extends_ given functionality, i.e. already defined functions (in either the main package or the one from the extension). It should not define new structs/functions.

When I wrote our extensions, having this in mind worked really well.  
E.g. you would define a new plot receipt or implement a new dispatch for another function once a certain package is present.

Sure we have one or two functions that default to an error since then a certain solver is not present – that could be considered plate code but is just 6 lines.

Can you maybe have a more precise example? I would think entirely new functionality (new functions names or structs) should be in a package not an extension.

---

<div class="post-metadata">

### Author: ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)
#### Post date: [August 30, 2023, 12:00pm UTC](https://discourse.julialang.org/t/should-we-define-new-functions-structs-in-an-extension/103361/3 "2023-08-30T12:00:16Z")

</div>

Ok, thanks, that makes sense. Probably good to make a clear distinction between the two use cases.

I have a concrete example in this [PR](https://github.com/JuliaTrustworthyAI/CounterfactualExplanations.jl/pull/270), where I turned `RCall`, `PythonCall` and `MPI` into extensions.

I think in the case of `MPI` I can live with the [boiler plate](https://github.com/JuliaTrustworthyAI/CounterfactualExplanations.jl/blob/914942b21b43caebd1e4659062e640eee4902f51/src/extensions/MPIExt.jl#L1). In the case of `PythonCall` (a lot of [boiler plate](https://github.com/JuliaTrustworthyAI/CounterfactualExplanations.jl/blob/914942b21b43caebd1e4659062e640eee4902f51/src/extensions/PythonCallExt.jl#L1)) and probably also `RCall` (some [boiler plate](https://github.com/JuliaTrustworthyAI/CounterfactualExplanations.jl/blob/914942b21b43caebd1e4659062e640eee4902f51/src/extensions/RCallExt.jl#L1)), it might make more sense to move to a separate package.

Thanks!

---

<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: [August 30, 2023, 1:05pm UTC](https://discourse.julialang.org/t/should-we-define-new-functions-structs-in-an-extension/103361/4 "2023-08-30T13:05:06Z")

</div>

Related docs PR

> <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: ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)
#### Post date: [August 30, 2023, 1:16pm UTC](https://discourse.julialang.org/t/should-we-define-new-functions-structs-in-an-extension/103361/5 "2023-08-30T13:16:21Z")

</div>

Thanks!!
