-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21441 from JuliaLang/vc/loading_callbacks
Add a callback to Base.require
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Module loading | ||
|
||
`Base.require`[@ref] is responsible for loading modules and it also manages the | ||
precompilation cache. It is the implementation of the `import` statement. | ||
|
||
## Experimental features | ||
The features below are experimental and not part of the stable Julia API. | ||
Before building upon them inform yourself about the current thinking and whether they might change soon. | ||
|
||
### Module loading callbacks | ||
|
||
It is possible to listen to the modules loaded by `Base.require`, by registering a callback. | ||
|
||
```julia | ||
loaded_packages = Channel{Symbol}() | ||
callback = (mod::Symbol) -> put!(loaded_packages, mod) | ||
push!(Base.package_callbacks, callback) | ||
``` | ||
|
||
Please note that the symbol given to the callback is a non-unique identifier and | ||
it is the responsibility of the callback provider to walk the module chain to | ||
determine the fully qualified name of the loaded binding. | ||
|
||
The callback below is an example of how to do that: | ||
|
||
```julia | ||
# Get the fully-qualified name of a module. | ||
function module_fqn(name::Symbol) | ||
fqn = Symbol[name] | ||
mod = getfield(Main, name) | ||
parent = Base.module_parent(mod) | ||
while parent !== Main | ||
push!(fqn, Base.module_name(parent)) | ||
parent = Base.module_parent(parent) | ||
end | ||
fqn = reverse!(fqn) | ||
return join(fqn, '.') | ||
end | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters