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
@@ -316,6 +316,68 @@ Here, Julia cannot decide which `f` you are referring to, so you have to make a
316
316
317
317
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).
318
318
319
+
### Precedence order of definitions
320
+
321
+
There are in general four kinds of binding definitions:
322
+
1. Those provided via implicit import through `using M`
323
+
2. Those provided via explicit import (e.g. `using M: x`, `import M: x`)
324
+
3. Those declared implicitly as global (via `global x` without type specification)
325
+
4. Those declared explicitly using definition syntax (`const`, `global x::T`, `struct`, etc.)
326
+
327
+
Syntactically, we divide these into three precedence levels (from weakest to strongest)
328
+
1. Implicit imports
329
+
2. Implicit declarations
330
+
3. Explicit declarations and imports
331
+
332
+
In general, we permit replacement of weaker bindings by stronger ones:
333
+
334
+
```julia-repl
335
+
julia> module M1; const x = 1; export x; end
336
+
Main.M1
337
+
338
+
julia> using .M1
339
+
340
+
julia> x # Implicit import from M1
341
+
1
342
+
343
+
julia> begin; f() = (global x; x = 1) end
344
+
345
+
julia> x # Implicit declaration
346
+
ERROR: UndefVarError: `x` not defined in `Main`
347
+
Suggestion: add an appropriate import or assignment. This global was declared but not assigned.
348
+
349
+
julia> const x = 2 # Explicit declaration
350
+
2
351
+
```
352
+
353
+
However, within the explicit precedence level, replacement is syntactically disallowed:
354
+
```julia-repl
355
+
julia> module M1; const x = 1; export x; end
356
+
Main.M1
357
+
358
+
julia> import .M1: x
359
+
360
+
julia> const x = 2
361
+
ERROR: cannot declare Main.x constant; it was already declared as an import
362
+
Stacktrace:
363
+
[1] top-level scope
364
+
@ REPL[3]:1
365
+
```
366
+
367
+
or ignored:
368
+
369
+
```julia-repl
370
+
julia> const y = 2
371
+
2
372
+
373
+
julia> import .M1: x as y
374
+
WARNING: import of M1.x into Main conflicts with an existing identifier; ignored.
375
+
```
376
+
377
+
The resolution of an implicit binding depends on the set of all `using`'d modules visible
378
+
in the current world age. See [the manual chapter on world age](@ref man-worldage) for more
379
+
details.
380
+
319
381
### Default top-level definitions and bare modules
320
382
321
383
Modules automatically contain `using Core`, `using Base`, and definitions of the [`eval`](@ref)
0 commit comments