Commit 955a12d6 authored by POTTIER Francois's avatar POTTIER Francois

Discuss ppx_import in the documentation.

parent 91b86b0a
Documentation: added an example of constructing a lexicographic ordering.
Documentation: discussed generating visitors for existing types and ppx_import.
Initial release.
......@@ -71,8 +71,10 @@
......@@ -708,7 +708,7 @@ decorates each node in an arithmetic expression with a unique integer number.%
\subsection{Dealing with preexisting types}
\subsection{Dealing with references to preexisting types}
A type definition can contain references to the types that are being defined,
......@@ -770,6 +770,46 @@ It is possible to inherit as many classes as one wishes, beyond those defined
in \oc|VisitorsRuntime|. This is done via the \ancestors parameter
% ------------------------------------------------------------------------------
\subsection{Generating visitors for preexisting types}
Because the \derivingvisitors annotation must be attached to the type
definition, it may seem as if it is impossible to generate a visitor for a
type whose definition is out of reach. Suppose, for instance, that the type
\oc|expr| of arithmetic expressions is defined in a module \oc|Expr|, which,
for some reason, we cannot modify. Can we generate a visitor for this type?
Fortunately, the answer is positive. The basic trick, documented in the
{\texttt{ppx\_deriving} \textsc{readme}},
consists in defining a new type \oc|expr| of arithmetic expressions and to
explicitly declare that it is equal to the preexisting type \oc|Expr.expr|,
as follows.
As can be seen above, the new definition of \oc|expr| can be annotated with
\derivingvisitors, yielding a visitor for the new type \oc|expr|, which by
definition, is equal to the preexisting type \oc|Expr.expr|. Thus, this
visitor class be used to traverse expressions of type \oc|Expr.expr|.
This approach works, but requires repeating the definition of the type \oc|expr|.
This duplication can be eliminated thanks to the \ppximport syntax extension,
as follows:
This expands to the code shown previously. (To use this syntax extension,
assuming you are using \ocamlbuild and \ocamlfind, just add the line
\texttt{true: package(ppx\_import)} to your \texttt{\_tags} file.) As icing on
the cake, \ppximport allows decorating the type definition, on the fly, with
new attributes. In the following examples, we replace all occurrences of
\oc|int| with \oc|int[@opaque]| (\sref{sec:opaque}), so as to ensure that the
generated visitor does not invoke the method \tyconvisitor{int}:
% ------------------------------------------------------------------------------
% ------------------------------------------------------------------------------
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment