# Extensions/weakdeps loading and package design

**URL:** <https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156>\
**Category:** General Usage\
**Tags:** package-extensions\
**Created:** [September 22, 2023, 3:33pm UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156 "2023-09-22T15:33:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 22, 2023, 3:33pm UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/1 "2023-09-22T15:33:36Z")

</div>

I just experimented for the first time the implementation of a package extension. Everything worked, but as I did it I need to load explicitly the weak dependency for the extension module to be loaded.

So my package looks like:

```julia
module MyPackage
    export foo
    function foo(args...; kargs...)
        error("Need to load StaticArrays for foo to work")
    end
end

module FooExt
    import MyPackage: foo
    using StaticArrays
    foo(x::SVector) = x
end

```

This works, but I did not find a documented indication that I need (or that is a good idea) to provide a sensible error message by defining a generic `foo(args...)` in the main package. Is that reasonable? Is there another default way to inform the user that the weakdep has to be loaded?

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 22, 2023, 8:01pm UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/2 "2023-09-22T20:01:26Z")

</div>

> [@lmiq](#):
>
> This works, but I did not find a documented indication that I need (or that is a good idea) to provide a sensible error message by defining a generic `foo(args...)` in the main package.

In your opinion, would the clarifications proposed in this PR have helped you understand how extensions are meant to be used?

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

The PR mentions the definition of an empty `function foo end`, but I think defining a generic fallback is a good idea in your case, because it gives the user actionable information about what to do: simply load the required package for the extension code to kick in.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 22, 2023, 8:13pm UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/3 "2023-09-22T20:13:42Z")

</div>

That is a nice addition to the docs. I maybe would be even more explicit and suggest the error message or a “not implemented method”.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 22, 2023, 8:16pm UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/4 "2023-09-22T20:16:58Z")

</div>

> [@lmiq](#):
>
> I maybe would be even more explicit and suggest the error message or a “not implemented method”.

@gdalle Do you have any thoughts on this?

---

<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, 9:22pm UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/5 "2023-09-22T21:22:16Z")

</div>

I’m torn.  
On the one hand, I usually prefer a `MethodError` to a hardcoded `error("this is not implemented")`, it is the natural result of a method not existing.  
On the other hand, I have often had the issue of an extension not loading, only diguised as a weird `MethodError` coming out of nowhere. An explicit mention of the extension in the error message would have helped me then.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 23, 2023, 9:36am UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/6 "2023-09-23T09:36:14Z")

</div>

I certainly prefer the most informative message and will use that in my package. If that is something to be recommended in general I’m not sure.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [September 23, 2023, 10:39am UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/7 "2023-09-23T10:39:48Z")

</div>

You could register an error hint for `MethodError`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 23, 2023, 11:36am UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/8 "2023-09-23T11:36:49Z")

</div>

What you mean by “register” there?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 23, 2023, 1:01pm UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/9 "2023-09-23T13:01:59Z")

</div>

[https://docs.julialang.org/en/v1/base/base/#Base.Experimental.register\_error\_hint](https://docs.julialang.org/en/v1/base/base/#Base.Experimental.register_error_hint)

This is still experimental, though

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 23, 2023, 3:54pm UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/10 "2023-09-23T15:54:02Z")

</div>

Seems more convoluted than the custom error message. What’s the advantage?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 24, 2023, 3:17am UTC](https://discourse.julialang.org/t/extensions-weakdeps-loading-and-package-design/104156/11 "2023-09-24T03:17:18Z")

</div>

> <https://github.com/jkrumbiegel/MakiePkgExtTest/blob/3fcb019c7c520aafe03fefdd52223d33923ffa52/SomePackage/src/SomePackage.jl#L27>

Here’s an example of such an error hint. The advantage is that you can be intentional about the MethodError which might under other circumstances signal a user error. But I agree it’s a bit convoluted.
