Skip to content

Commit

Permalink
Improve enumerate macro (#8819)
Browse files Browse the repository at this point in the history
* fix case macro manual entry to produce code block

Previously line breaks were so weird that the code blocks were not created.

* improve `enumerate` for loop macro by wrapping in block
  • Loading branch information
Vindaar authored and Araq committed Aug 30, 2018
1 parent 2f7b979 commit 47c7fd0
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions doc/manual.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5459,12 +5459,17 @@ type ``system.ForLoopStmt`` can rewrite the entirety of a ``for`` loop:
newFor.add x[^2][1] newFor.add x[^2][1]
newFor.add body newFor.add body
result.add newFor result.add newFor
# now wrap the whole macro in a block to create a new scope
result = quote do:
block: `result`
for a, b in enumerate(items([1, 2, 3])): for a, b in enumerate(items([1, 2, 3])):
echo a, " ", b echo a, " ", b
for a2, b2 in enumerate([1, 2, 3, 5]): # without wrapping the macro in a block, we'd need to choose different
echo a2, " ", b2 # names for `a` and `b` here to avoid redefinition errors
for a, b in enumerate([1, 2, 3, 5]):
echo a, " ", b
Currently for loop macros must be enabled explicitly Currently for loop macros must be enabled explicitly
Expand All @@ -5474,12 +5479,11 @@ via ``{.experimental: "forLoopMacros".}``.
Case statement macros Case statement macros
--------------------- ---------------------


A macro that needs to be called `match`:idx: can be used to A macro that needs to be called `match`:idx: can be used to rewrite
rewrite ``case`` statements in order to ``case`` statements in order to implement `pattern matching`:idx: for
implement `pattern matching`:idx: for certain types. The following certain types. The following example implements a simplistic form of
example implements a simplistic form of pattern matching for tuples, pattern matching for tuples, leveraging the existing equality operator
leveraging the existing equality operator for tuples (as provided in for tuples (as provided in ``system.==``):
``system.==``):


.. code-block:: nim .. code-block:: nim
:test: "nim c $1" :test: "nim c $1"
Expand Down

0 comments on commit 47c7fd0

Please sign in to comment.