summary refs log tree commit diff stats
path: root/doc/manual.rst
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2019-03-27 15:08:23 +0100
committerAndreas Rumpf <rumpf_a@web.de>2019-03-27 15:08:32 +0100
commitfcd3b0c4d8f266ca60aa0b2afe1f00bb68b4a9a4 (patch)
treeee0ecb78c3f71f0d2d03a58d15c104a47cd3aca7 /doc/manual.rst
parent3cb645ab500487b62d82c9d02c0e7f21b2cf1413 (diff)
downloadNim-fcd3b0c4d8f266ca60aa0b2afe1f00bb68b4a9a4.tar.gz
manual: prefer 'typeof(x)' over 'type(x)'
Diffstat (limited to 'doc/manual.rst')
-rw-r--r--doc/manual.rst36
1 files changed, 20 insertions, 16 deletions
diff --git a/doc/manual.rst b/doc/manual.rst
index 8771f8fe9..d9112652b 100644
--- a/doc/manual.rst
+++ b/doc/manual.rst
@@ -5153,35 +5153,39 @@ types that will match the type param:
 
 The constraint can be a concrete type or a type class.
 
-type operator
--------------
 
-You can obtain the type of a given expression by constructing a ``type``
+typeof operator
+---------------
+
+**Note**: ``typeof(x)`` can also be written as ``type(x)``.
+
+You can obtain the type of a given expression by constructing a ``typeof``
 value from it (in many other languages this is known as the `typeof`:idx:
 operator):
 
 .. code-block:: nim
-  var x = 0
-  var y: type(x) # y has type int
-
-You may add a constraint to the resulting type to trigger a compile-time error
-if the expression doesn't have the expected type:
 
-.. code-block:: nim
   var x = 0
-  var y: type[object](x) # Error: type mismatch: got <int> but expected 'object'
+  var y: typeof(x) # y has type int
 
-If ``type`` is used to determine the result type of a proc/iterator/converter
+
+If ``typeof`` is used to determine the result type of a proc/iterator/converter
 call ``c(X)`` (where ``X`` stands for a possibly empty list of arguments), the
 interpretation where ``c`` is an iterator is preferred over the
-other interpretations:
+other interpretations, but this behavior can be changed by
+passing ``typeOfProc`` as the second argument to ``typeof``:
 
 .. code-block:: nim
-  import strutils
+    :test: "nim c $1"
+
+  iterator split(s: string): string = discard
+  proc split(s: string): seq[string] = discard
+
+  # since an iterator is the preferred interpretation, `y` has the type ``string``:
+  assert typeof("a b c".split) is string
+
+  assert typeof("a b c".split, typeOfProc) is seq[string]
 
-  # strutils contains both a ``split`` proc and iterator, but since an
-  # an iterator is the preferred interpretation, `y` has the type ``string``:
-  var y: type("a b c".split)
 
 
 Modules