Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: how we write to_dyn and equal #6621

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions doc/hacking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,30 @@ Good:
- Do not raise ``Invalid_argument``. Instead, raise with ``Code_error.raise``
which allows to attach more informative payloads than just strings.

- To write a ``to_dyn`` function on a record type, use the following pattern. It
ensures that the pattern matching will break when a field is added. To ignore
a field, add ``; d = _``, not ``; _``.

.. code:: ocaml

let to_dyn {a; b; c} =
Dyn.record
[ ("a", A.to_dyn a)
; ("b", B.to_dyn b)
; ("c", C.to_dyn c)
]

- To write an equality function, use the following pattern (this applies to
other kinds of binary functions). The same remarks about about pattern
matching and ignoring fields apply.

.. code:: ocaml

let equal {a; b; c} t =
A.equal a t.a &&
B.equal b t.b &&
C.equal c t.c

Subjective Style Points
-----------------------

Expand Down