Skip to content

Commit

Permalink
Document the required order of &-parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed Jul 11, 2018
1 parent 9cc9036 commit 9859b00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
2 changes: 2 additions & 0 deletions NEWS.rst
Expand Up @@ -41,6 +41,8 @@ Other Breaking Changes
* `HySymbol` no longer inherits from `HyString`. * `HySymbol` no longer inherits from `HyString`.
* `(except)` is no longer allowed. Use `(except [])` instead. * `(except)` is no longer allowed. Use `(except [])` instead.
* `(import [foo])` is no longer allowed. Use `(import foo)` instead. * `(import [foo])` is no longer allowed. Use `(import foo)` instead.
* `&`-parameters in lambda lists must now appear in the same order that
Python expects.


New Features New Features
------------------------------ ------------------------------
Expand Down
47 changes: 25 additions & 22 deletions docs/language/api.rst
Expand Up @@ -457,7 +457,11 @@ and the *body* of the function:
(defn name [params] body) (defn name [params] body)
Parameters may have the following keywords in front of them: Parameters may be prefixed with the following special symbols. If you use more
than one, they can only appear in the given order (so all `&optional`
parameters must precede any `&rest` parameter, `&rest` must precede `&kwonly`,
and `&kwonly` must precede `&kwargs`). This is the same order that Python
requires.


&optional &optional
Parameter is optional. The parameter can be given as a two item list, where Parameter is optional. The parameter can be given as a two item list, where
Expand All @@ -476,26 +480,6 @@ Parameters may have the following keywords in front of them:
=> (total-value 100 1) => (total-value 100 1)
101.0 101.0
&kwargs
Parameter will contain 0 or more keyword arguments.

The following code examples defines a function that will print all keyword
arguments and their values.

.. code-block:: clj
=> (defn print-parameters [&kwargs kwargs]
... (for [(, k v) (.items kwargs)] (print k v)))
=> (print-parameters :parameter-1 1 :parameter-2 2)
parameter_1 1
parameter_2 2
; to avoid the mangling of '-' to '_', use unpacking:
=> (print-parameters #** {"parameter-1" 1 "parameter-2" 2})
parameter-1 1
parameter-2 2
&rest &rest
Parameter will contain 0 or more positional arguments. No other positional Parameter will contain 0 or more positional arguments. No other positional
arguments may be specified after this one. arguments may be specified after this one.
Expand All @@ -517,7 +501,7 @@ Parameters may have the following keywords in front of them:
8 8
=> (zig-zag-sum 1 2 3 4 5 6) => (zig-zag-sum 1 2 3 4 5 6)
-3 -3
&kwonly &kwonly
.. versionadded:: 0.12.0 .. versionadded:: 0.12.0


Expand Down Expand Up @@ -553,6 +537,25 @@ Parameters may have the following keywords in front of them:
Availability: Python 3. Availability: Python 3.


&kwargs
Parameter will contain 0 or more keyword arguments.

The following code examples defines a function that will print all keyword
arguments and their values.

.. code-block:: clj
=> (defn print-parameters [&kwargs kwargs]
... (for [(, k v) (.items kwargs)] (print k v)))
=> (print-parameters :parameter-1 1 :parameter-2 2)
parameter_1 1
parameter_2 2
; to avoid the mangling of '-' to '_', use unpacking:
=> (print-parameters #** {"parameter-1" 1 "parameter-2" 2})
parameter-1 1
parameter-2 2
defn/a defn/a
------ ------
Expand Down

0 comments on commit 9859b00

Please sign in to comment.