summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2015-01-26 22:48:11 +0100
committerAraq <rumpf_a@web.de>2015-01-27 01:36:20 +0100
commitd08cec0f7deb83ed78023ca0c81d38511f144b00 (patch)
treecbf9fd33e95eed3a024a2987636ae765b314fd4f /doc
parent26b853923cbbd92b9b74cf1b9dce773b0ff39cf3 (diff)
downloadNim-d08cec0f7deb83ed78023ca0c81d38511f144b00.tar.gz
better documentation for 'finished' for first class iterators
Diffstat (limited to 'doc')
-rw-r--r--doc/manual/procs.txt34
-rw-r--r--doc/manual/types.txt17
2 files changed, 51 insertions, 0 deletions
diff --git a/doc/manual/procs.txt b/doc/manual/procs.txt
index a8a2d271d..f48dfc6b9 100644
--- a/doc/manual/procs.txt
+++ b/doc/manual/procs.txt
@@ -556,6 +556,40 @@ The builtin ``system.finished`` can be used to determine if an iterator has
 finished its operation; no exception is raised on an attempt to invoke an
 iterator that has already finished its work.
 
+Note that ``system.finished`` is error prone to use because it only returns
+``true`` one iteration after the iterator has finished:
+
+.. code-block:: nim
+  iterator mycount(a, b: int): int {.closure.} =
+    var x = a
+    while x <= b:
+      yield x
+      inc x
+
+  var c = mycount # instantiate the iterator
+  while not finished(c):
+    echo c(1, 3)
+
+  # Produces
+  1
+  2
+  3
+  0
+
+Instead this code has be used:
+
+.. code-block:: nim
+  var c = mycount # instantiate the iterator
+  while true:
+    let value = c(1, 3)
+    if finished(c): break # and discard 'value'!
+    echo value
+
+It helps to think that the iterator actually returns a
+pair ``(value, done)`` and ``finished`` is used to access the hidden ``done``
+field.
+
+
 Closure iterators are *resumable functions* and so one has to provide the
 arguments to every call. To get around this limitation one can capture
 parameters of an outer factory proc:
diff --git a/doc/manual/types.txt b/doc/manual/types.txt
index e2595cb71..32ff19f75 100644
--- a/doc/manual/types.txt
+++ b/doc/manual/types.txt
@@ -681,6 +681,23 @@ dereferencing operations for reference types:
   n.data = 9
   # no need to write n[].data; in fact n[].data is highly discouraged!
 
+Automatic dereferencing is also performed for the first argument of a routine
+call. But currently this feature has to be only enabled
+via ``{.experimental.}``:
+
+.. code-block:: nim
+  {.experimental.}
+
+  proc depth(x: NodeObj): int = ...
+
+  var
+    n: Node
+  new(n)
+  echo n.depth
+  # no need to write n[].depth either
+  
+
+
 In order to simplify structural type checking, recursive tuples are not valid:
 
 .. code-block:: nim