hy/docs/contrib/hy_repr.rst

77 lines
2.1 KiB
ReStructuredText
Raw Normal View History

2017-03-20 20:04:00 +01:00
==================
Hy representations
==================
.. versionadded:: 0.13.0
2017-11-03 22:03:02 +01:00
``hy.contrib.hy-repr`` is a module containing two functions.
To import them, say::
2017-11-03 22:03:02 +01:00
(import [hy.contrib.hy-repr [hy-repr hy-repr-register]])
To make the Hy REPL use it for output, invoke Hy like so::
$ hy --repl-output-fn=hy.contrib.hy-repr.hy-repr
2017-03-20 20:04:00 +01:00
.. _hy-repr-fn:
hy-repr
-------
Usage: ``(hy-repr x)``
This function is Hy's equivalent of Python's built-in ``repr``.
It returns a string representing the input object in Hy syntax.
.. code-block:: hy
=> (hy-repr [1 2 3])
'[1 2 3]'
=> (repr [1 2 3])
'[1, 2, 3]'
2017-11-03 22:03:02 +01:00
Like ``repr`` in Python, ``hy-repr`` can round-trip many kinds of
values. Round-tripping implies that given an object ``x``,
``(eval (read-str (hy-repr x)))`` returns ``x``, or at least a value
that's equal to ``x``.
.. _hy-repr-register-fn:
hy-repr-register
----------------
Usage: ``(hy-repr-register the-type fun)``
``hy-repr-register`` lets you set the function that ``hy-repr`` calls to
represent a type.
2017-03-20 20:04:00 +01:00
.. code-block:: hy
2017-11-03 22:03:02 +01:00
=> (defclass C)
=> (hy-repr-register C (fn [x] "cuddles"))
=> (hy-repr [1 (C) 2])
'[1 cuddles 2]'
2017-03-20 20:04:00 +01:00
2017-11-03 22:03:02 +01:00
If the type of an object passed to ``hy-repr`` doesn't have a registered
function, ``hy-repr`` will search the type's method resolution order
(its ``__mro__`` attribute) for the first type that does. If ``hy-repr``
doesn't find a candidate, it falls back on ``repr``.
2017-03-20 20:04:00 +01:00
2017-11-03 22:03:02 +01:00
Registered functions often call ``hy-repr`` themselves. ``hy-repr`` will
automatically detect self-references, even deeply nested ones, and
output ``"..."`` for them instead of calling the usual registered
function. To use a placeholder other than ``"..."``, pass a string of
your choice to the keyword argument ``:placeholder`` of
``hy-repr-register``.
.. code-block:: hy
(defclass Container [object]
2019-07-02 18:16:06 +02:00
(defn __init__ (fn [self value]
(setv self.value value))))
2017-11-03 22:03:02 +01:00
(hy-repr-register Container :placeholder "HY THERE" (fn [x]
(+ "(Container " (hy-repr x.value) ")")))
(setv container (Container 5))
(setv container.value container)
(print (hy-repr container)) ; Prints "(Container HY THERE)"