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 24, 2016
1 parent 0df6e61 commit 5276a4d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions doc/manual/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,22 @@ 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 computing state for
the local machine, which does not need to be -- or even should not be --
captured in the compiled image.
It will be called after the module is loaded into a process,
including if it is being loaded into an incremental compile
(``--output-incremental=yes``), but not if it is being loaded
into a full-compilation process.

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 +323,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 5276a4d

Please sign in to comment.