Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
visitors
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
POTTIER Francois
visitors
Commits
72759510
Commit
72759510
authored
Mar 17, 2017
by
POTTIER Francois
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Documented [@build].
parent
7527f247
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
doc/main.tex
doc/main.tex
+62
-0
No files found.
doc/main.tex
View file @
72759510
...
...
@@ -1092,6 +1092,7 @@ from \oc|VisitorsRuntime|. This is done via the \nude parameter
% ------------------------------------------------------------------------------
\subsection
{
Generating visitors for preexisting types
}
\label
{
sec:import
}
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
...
...
@@ -2242,6 +2243,67 @@ If desired, instead of \oc|[@name]|, one can use
% ------------------------------------------------------------------------------
\subsection
{
Alternate construction code in
\map
visitors
}
\label
{
sec:build
}
By default, a
\map
visitor for an algebraic data type constructs a new data
structure, by applying a data constructor or by allocating a new record.
Sometimes, though, it is illegal to do so: this is the case, for instance, if
the type has been declared
\oc
|private|.
%
% Sometimes, the default behavior is well-typed, but is not the desired
% behavior. In that case, it can be customized by overriding a method. Using
% [@build] offers a little extra conciseness and efficiency in that case, but
% this need not be advertised.
%
% Another solution would be to just write the visitor by hand for a private
% type, but that would require more work from the user.
%
In such a situation, an alternate constructor can be provided via a
\oc
|
[
@build
]
| attribute
(
which must be attached to a data constructor
)
or via a
\oc
|
[
@@build
]
| attribute
(
which must be attached to a record type declaration
)
.
%
Such an attribute carries an OCaml expression, which should represent a
constructor function.
For instance, suppose that the module
\oc
|Point| has the following signature:
\begin
{
mdframed
}
[
backgroundcolor
=
green
!
10
]
\lstinputlisting
{
point.mli
}
\end
{
mdframed
}
Suppose that, outside of this module, one wishes to generate a
\map
visitor
for the type
\oc
|Point.point|. One would like to do this in the style that was
presented earlier
(
\sref
{
sec:import
}
)
, but a naïve attempt fails: the
generated visitor is illegal, because allocating a record of type
\oc
|Point.point| is forbidden. To circumvent this problem, one should
indicate, via a
\oc
|
[
@@build
]
| attribute, that the constructor function
\oc
|Point.make| should be used:
%
\begin
{
origenv
}
\begin
{
lstlisting
}
type point
=
Point.point
=
private
{
x: int; y: int
}
[
@@build Point.make
]
[
@@deriving visitors
{
variety
=
"map"
}
]
\end
{
lstlisting
}
\end
{
origenv
}
%
The OCaml expression carried by a
\oc
|
[
@build
]
| attribute can refer to the
local variable
\oc
|self|, which represents the visitor object, and to the
local variable
\oc
|env|, which represents the environment received by the
visitor method.
\oc
|
[
@build
]
| attributes influence not only
\map
visitors,
but also
\mapendo
and
\mapreduce
visitors.
Instead of
\oc
|
[
@build
]
|, one can use
\oc
|
[
@visitors.build
]
| or
\oc
|
[
@deriving.visitors.build
]
|.
% Be careful not to misspell the attribute name,
% as the attribute would then be silently ignored.
% ------------------------------------------------------------------------------
\bibliographystyle
{
plain
}
\bibliography
{
english,local
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment