You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/differencesfromclojure.rst
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -166,8 +166,9 @@ Host interoperability features generally match those of Clojure.
166
166
Type Hinting
167
167
^^^^^^^^^^^^
168
168
169
-
Type hints may be applied anywhere they are supported in Clojure, but the compiler does not currently use them.
170
-
Support for attaching type hints to the Python AST is tracked in `#354 <https://github.com/basilisp-lang/basilisp/issues/354>`_\.
169
+
Type hints may be applied anywhere they are supported in Clojure (as the ``:tag`` metadata key), but the compiler does not currently use them for any purpose.
170
+
Tags provided for ``def`` names, function arguments and return values, and :lpy:form:`let` locals will be applied to the resulting Python AST by the compiler wherever possible.
171
+
Particularly in the case of function arguments and return values, these tags maybe introspected from the Python `inspect <https://docs.python.org/3/library/inspect.html>`_ module.
171
172
There is no need for type hints anywhere in Basilisp right now, however.
Copy file name to clipboardExpand all lines: docs/pyinterop.rst
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -211,3 +211,37 @@ As you can see in the example above, this strategy fits neatly with the existing
211
211
The ``:collect`` strategy is a better accompaniment to functions with positional arguments.
212
212
With this strategy, Python keyword arguments are converted into a Basilisp map with de-munged keyword arguments and passed as the final positional argument of the function.
213
213
You can use map destructuring on this final positional argument, just as you would with the map in the ``:apply`` case above.
214
+
215
+
Type Hinting
216
+
------------
217
+
218
+
Basilisp supports passing type hints through to the underlying generated Python using type hints by applying the ``:tag`` metadata to certain syntax elements.
219
+
220
+
In Clojure, these tags are type declarations for certain primitive types.
221
+
In Clojurescript, tags are type *hints* and they are only necessary in extremely limited circumstances to help the compiler.
222
+
In Basilisp, tags are not used by the compiler at all.
223
+
Instead, tags applied to function arguments and return values in Basilisp are applied to the underlying Python objects and are introspectable at runtime using the Python `inspect <https://docs.python.org/3/library/inspect.html>`_ standard library module.
224
+
225
+
Type hints may be applied to :lpy:form:`def` names, function arguments and return values, and :lpy:form:`let` local forms.
226
+
227
+
.. code-block:: clojure
228
+
229
+
(def ^python/str s "a string")
230
+
231
+
(defn upper
232
+
^python/str [^python/str s]
233
+
(.upper s))
234
+
235
+
(let [^python/int i 64]
236
+
(* i 2))
237
+
238
+
.. note::
239
+
240
+
The reader applies ``:tag`` :ref:`metadata` automatically for symbols following the ``^`` symbol, but users may manually apply ``:tag`` metadata containing any valid expression.
241
+
Python permits any valid expression in a variable annotation, so Basilisp likewise allows any valid expression.
242
+
243
+
.. warning::
244
+
245
+
Due to the complexity of supporting multi-arity functions in Python, only return annotations are preserved on the arity dispatch function.
246
+
Return annotations are combined as by ``typing.Union``, so ``typing.Union[str, str] == str``.
247
+
The annotations for individual arity arguments are preserved in their compiled form, but they are challenging to access programmatically.
0 commit comments