You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/creating-packages.md
+75Lines changed: 75 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -245,6 +245,81 @@ using Test
245
245
Every dependency should in general have a compatibility constraint on it.
246
246
This is an important topic so there is a separate chapter about it: [Compatibility](@ref Compatibility).
247
247
248
+
## Conditional loading of code in packages (Glue packages)
249
+
250
+
!!! note
251
+
This is a somewhat advanced section which can be skipped for people new to Julia and Julia packages.
252
+
253
+
It is sometimes desirable to be able to extend some functionality of a package without having to
254
+
unconditionally take on the cost (in terms of e.g. load time) of adding an extra dependency.
255
+
A *glue package* is a file that gets automatically loaded when some other set of packages are
256
+
loaded into the Julia session.
257
+
258
+
A useful application of glue packages could be for a plotting package that should be able to plot
259
+
objects from a wide variety of different Julia packages.
260
+
Adding all those different Julia packages as dependencies
261
+
could be expensive since they would end up getting loaded even if they were never used.
262
+
Instead, these code required to plot objects for specific packages can be put into separate files
263
+
(glue packages) which is only loaded when
264
+
265
+
Below is an example of how the code can be structured for a use case as outlined above.
266
+
267
+
`Project.toml`:
268
+
```toml
269
+
name = "Plotting"
270
+
version = "0.1.0"
271
+
uuid = "..."
272
+
273
+
[deps]
274
+
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
275
+
276
+
[gluedeps]
277
+
Contour = "d38c429a-6771-53c6-b99e-75d170b6e991"
278
+
279
+
[gluepkgs]
280
+
# name of glue package to the left
281
+
# glue dependencies required to load the glue pkg to the right
282
+
# use a list for multiple glue dependencies
283
+
GlueContour = "Contour"
284
+
285
+
[compat] # compat can also be given on glue dependencies
286
+
Colors = "0.12.8"
287
+
Contour = "0.6.2"
288
+
```
289
+
290
+
`src/Plotting.jl`:
291
+
```julia
292
+
module Plotting
293
+
using Colors
294
+
295
+
functionplot(x::Colors.Color)
296
+
# Some functionality for plotting a color here
297
+
end
298
+
299
+
end# module
300
+
```
301
+
302
+
`glue/GlueContour.jl` (can also be in `glue/GlueContour/GlueContour.jl`):
303
+
```julia
304
+
module GlueContour
305
+
306
+
using Plotting, Contour
307
+
308
+
function Plotting.plot(c::Contour.ContourCollection)
309
+
# Some functionality for plotting a contour here
310
+
end
311
+
312
+
end# module
313
+
```
314
+
315
+
A user that depends on `Plotting` will not pay the code of the "glue code" inside the `GlueContour` module.
316
+
It is only when the `Contour` package actually gets loaded that the `GlueCountour` glue package will get loaded
317
+
and provide the new functionality.
318
+
319
+
Compatibility can be set on glue dependencies just like normal dependencies.
320
+
321
+
A glue package will only be loaded if the glue dependencies are loaded from the same environment or environments higher in the environment stack than the package itself.
322
+
248
323
## Package naming guidelines
249
324
250
325
Package names should be sensible to most Julia users, *even to those who are not domain experts*.
0 commit comments