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: doc/src/manual/modules.md
+62Lines changed: 62 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -322,6 +322,68 @@ Here, Julia cannot decide which `f` you are referring to, so you have to make a
322
322
323
323
3. When the names in question *do* share a meaning, it is common for one module to import it from another, or have a lightweight “base” package with the sole function of defining an interface like this, which can be used by other packages. It is conventional to have such package names end in `...Base` (which has nothing to do with Julia's `Base` module).
324
324
325
+
### Precedence order of definitions
326
+
327
+
There are in general four kinds of binding definitions:
328
+
1. Those provided via implicit import through `using M`
329
+
2. Those provided via explicit import (e.g. `using M: x`, `import M: x`)
330
+
3. Those declared implicitly as global (via `global x` without type specification)
331
+
4. Those declared explicitly using definition syntax (`const`, `global x::T`, `struct`, etc.)
332
+
333
+
Syntactically, we divide these into three precedence levels (from weakest to strongest)
334
+
1. Implicit imports
335
+
2. Implicit declarations
336
+
3. Explicit declarations and imports
337
+
338
+
In general, we permit replacement of weaker bindings by stronger ones:
339
+
340
+
```julia-repl
341
+
julia> module M1; const x = 1; export x; end
342
+
Main.M1
343
+
344
+
julia> using .M1
345
+
346
+
julia> x # Implicit import from M1
347
+
1
348
+
349
+
julia> begin; f() = (global x; x = 1) end
350
+
351
+
julia> x # Implicit declaration
352
+
ERROR: UndefVarError: `x` not defined in `Main`
353
+
Suggestion: add an appropriate import or assignment. This global was declared but not assigned.
354
+
355
+
julia> const x = 2 # Explicit declaration
356
+
2
357
+
```
358
+
359
+
However, within the explicit precedence level, replacement is syntactically disallowed:
360
+
```julia-repl
361
+
julia> module M1; const x = 1; export x; end
362
+
Main.M1
363
+
364
+
julia> import .M1: x
365
+
366
+
julia> const x = 2
367
+
ERROR: cannot declare Main.x constant; it was already declared as an import
368
+
Stacktrace:
369
+
[1] top-level scope
370
+
@ REPL[3]:1
371
+
```
372
+
373
+
or ignored:
374
+
375
+
```julia-repl
376
+
julia> const y = 2
377
+
2
378
+
379
+
julia> import .M1: x as y
380
+
WARNING: import of M1.x into Main conflicts with an existing identifier; ignored.
381
+
```
382
+
383
+
The resolution of an implicit binding depends on the set of all `using`'d modules visible
384
+
in the current world age. See [the manual chapter on world age](@ref man-worldage) for more
385
+
details.
386
+
325
387
### Default top-level definitions and bare modules
326
388
327
389
Modules automatically contain `using Core`, `using Base`, and definitions of the [`eval`](@ref)
0 commit comments