Skip to content

Commit ee09ae7

Browse files
KristofferCDilumAluthgeMasonProtterfingolfinmbauman
authored
recommend explicit using Foo: Foo, ... in package code (was: "using considered harmful") (#42080)
I feel we are heading up against a "`using` crisis" where any new feature that is implemented by exporting a new name (either in Base or a package) becomes a breaking change. This is already happening (JuliaGPU/CUDA.jl#1097, JuliaWeb/HTTP.jl#745) and as projects get bigger and more names are exported, the likelihood of this rapidly increases. The flaw in `using Foo` is fundamental in that you cannot lexically see where a name comes from so when two packages export the same name, you are screwed. Any code that relies on `using Foo` and then using an exported name from `Foo` is vulnerable to another dependency exporting the same name. Therefore, I think we should start to strongly discourage the use of `using Foo` and only recommend `using Foo` for ephemeral work (e.g. REPL work). --------- Co-authored-by: Dilum Aluthge <dilum@aluthge.com> Co-authored-by: Mason Protter <mason.protter@icloud.com> Co-authored-by: Max Horn <max@quendi.de> Co-authored-by: Matt Bauman <mbauman@juliahub.com> Co-authored-by: Alex Arslan <ararslan@comcast.net> Co-authored-by: Ian Butterworth <i.r.butterworth@gmail.com> Co-authored-by: Neven Sajko <s@purelymail.com>
1 parent f6a38e0 commit ee09ae7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

base/docs/basedocs.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ kw"help", kw"Julia", kw"julia", kw""
3737
available for direct use. Names can also be used via dot syntax (e.g. `Foo.foo` to access
3838
the name `foo`), whether they are `export`ed or not.
3939
See the [manual section about modules](@ref modules) for details.
40+
41+
!!! note
42+
When two or more packages/modules export a name and that name does not refer to the
43+
same thing in each of the packages, and the packages are loaded via `using` without
44+
an explicit list of names, it is an error to reference that name without qualification.
45+
It is thus recommended that code intended to be forward-compatible with future versions
46+
of its dependencies and of Julia, e.g., code in released packages, list the names it
47+
uses from each loaded package, e.g., `using Foo: Foo, f` rather than `using Foo`.
4048
"""
4149
kw"using"
4250

doc/src/manual/modules.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ VERSION >= v"1.11.0-DEV.469" && eval(Meta.parse("public a, b, c"))
116116

117117
### Standalone `using` and `import`
118118

119-
Possibly the most common way of loading a module is `using ModuleName`. This [loads](@ref
119+
For interactive use, the most common way of loading a module is `using ModuleName`. This [loads](@ref
120120
code-loading) the code associated with `ModuleName`, and brings
121121

122122
1. the module name
@@ -172,6 +172,13 @@ Importantly, the module name `NiceStuff` will *not* be in the namespace. If you
172172
julia> using .NiceStuff: nice, DOG, NiceStuff
173173
```
174174

175+
When two or more packages/modules export a name and that name does not refer to the
176+
same thing in each of the packages, and the packages are loaded via `using` without
177+
an explicit list of names, it is an error to reference that name without qualification.
178+
It is thus recommended that code intended to be forward-compatible with future versions
179+
of its dependencies and of Julia, e.g., code in released packages, list the names it
180+
uses from each loaded package, e.g., `using Foo: Foo, f` rather than `using Foo`.
181+
175182
Julia has two forms for seemingly the same thing because only `import ModuleName: f` allows adding methods to `f`
176183
*without a module path*.
177184
That is to say, the following example will give an error:

0 commit comments

Comments
 (0)