Skip to content

Commit

Permalink
document __init__ behavior during precompile
Browse files Browse the repository at this point in the history
fix #18115
  • Loading branch information
vtjnash committed Aug 23, 2016
1 parent 9548090 commit ba9a921
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions doc/manual/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,20 @@ initialization steps that must occur at *runtime* from steps that can
occur at *compile time*. For this purpose, Julia allows you to define
an ``__init__()`` function in your module that executes any
initialization steps that must occur at runtime.
This function will not be called during compilation
(``--output-*`` or ``__precompile__()``).
You may, of course, call it manually if necessary,
but the default is to assume this function deals with local state
which does not need to be captured in the compiled image.
And it will be called anytime the module is loaded,
including if it is being looded into an incremental compile
(``--output-incremental=yes``), but not if it is being loaded into a full compile.

In particular, if you define a ``function __init__()`` in a module,
then Julia will call ``__init__()`` immediately *after* the module is
loaded (e.g., by ``import``, ``using``, or ``require``) at runtime for
the *first* time (i.e., ``__init__`` is only called once, and only
after all statements in the module have been executed). Because it is
after all statements in the module have been executed). Because it is
called after the module is fully imported, any submodules or other
imported modules have their ``__init__`` functions called *before* the
``__init__`` of the enclosing module.
Expand All @@ -313,7 +321,7 @@ Two typical uses of ``__init__`` are calling runtime initialization
functions of external C libraries and initializing global constants
that involve pointers returned by external libraries. For example,
suppose that we are calling a C library ``libfoo`` that requires us
to call a ``foo_init()`` initialization function at runtime. Suppose
to call a ``foo_init()`` initialization function at runtime. Suppose
that we also want to define a global constant ``foo_data_ptr`` that
holds the return value of a ``void *foo_data()`` function defined by
``libfoo`` — this constant must be initialized at runtime (not at compile
Expand Down

0 comments on commit ba9a921

Please sign in to comment.