# Best practice for not requiring users to load package for package extensions?

**URL:** <https://discourse.julialang.org/t/best-practice-for-not-requiring-users-to-load-package-for-package-extensions/102316>\
**Category:** General Usage\
**Tags:** best-practices, package-extensions\
**Created:** [July 31, 2023, 4:01pm UTC](https://discourse.julialang.org/t/best-practice-for-not-requiring-users-to-load-package-for-package-extensions/102316 "2023-07-31T16:01:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![M-PERSIC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-persic/32/51968_2.png) [@M-PERSIC](https://discourse.julialang.org/u/M-PERSIC)\
**Post date:** [July 31, 2023, 4:01pm UTC](https://discourse.julialang.org/t/best-practice-for-not-requiring-users-to-load-package-for-package-extensions/102316/1 "2023-07-31T16:01:49Z")

</div>

I am looking to make loading package extensions transparent to the user of a package, meaning they would not have to manually include the conditional dependency to their environment. For example, if the user calls a function with a specific option enabled, then that function would add the dependency for the user:

```julia
function a(x::Bool)
	if x
		using Pkg
		Pkg.add("package")
		if !isdefined(Base, :get_extension)
  			include("../ext/PackageExt.jl")
  		end
	end
end

```

I’m wondering if the use of Pkg in this instance is considered best practice or if there is a more idiomatic approach for such.

---

<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:** [July 31, 2023, 6:31pm UTC](https://discourse.julialang.org/t/best-practice-for-not-requiring-users-to-load-package-for-package-extensions/102316/2 "2023-07-31T18:31:53Z")

</div>

While I understand the intent, I think leaving control of the dependencies to the user is a fairly reasonable thing to do. Automatic install is not the norm in the Julia ecosystem, and using Pkg is so simple that it probably should stay that way.  
Maybe provide a useful error message in the default implementation of the function, and overload said implementation with an extension method?

---

<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:** [July 31, 2023, 6:54pm UTC](https://discourse.julialang.org/t/best-practice-for-not-requiring-users-to-load-package-for-package-extensions/102316/3 "2023-07-31T18:54:48Z")

</div>

I would add that decoupling package installation from usage is one of the things that improved user experience in Julia a lot. People is willing to wait for packages to install much more than what they are willing to wait functions to work when they try. And with code cashing the difference of both steps increased a lot (one is much slower, the other much faster), so the user will notice that even further.

---

<div class="post-metadata">

**Author:** ![M-PERSIC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-persic/32/51968_2.png) [@M-PERSIC](https://discourse.julialang.org/u/M-PERSIC)\
**Post date:** [August 4, 2023, 3:33pm UTC](https://discourse.julialang.org/t/best-practice-for-not-requiring-users-to-load-package-for-package-extensions/102316/4 "2023-08-04T15:33:07Z")

</div>

Thank you for both of your answers. I’ll adopt the error pattern.
