Mentions tuple unpacking only works in var/let blocks.

This commit is contained in:
Grzegorz Adam Hankiewicz 2013-12-01 21:07:50 +01:00
commit d1284ff33d

View file

@ -1361,13 +1361,13 @@ Even though you don't need to declare a type for a tuple to use it, tuples
created with different field names will be considered different objects despite created with different field names will be considered different objects despite
having the same field types. having the same field types.
Tuples can be *unpacked* during variable assignment. This can be handy to Tuples can be *unpacked* during variable assignment (and only then!). This can
assign directly the fields of the tuples to individually named variables. An be handy to assign directly the fields of the tuples to individually named
example of this is the ``splitFile`` proc from the `os module <os.html>`_ which variables. An example of this is the ``splitFile`` proc from the `os module
returns the directory, name and extension of a path at the same time. For tuple <os.html>`_ which returns the directory, name and extension of a path at the
unpacking to work you have to use parenthesis around the values you want to same time. For tuple unpacking to work you have to use parenthesis around the
assign the unpacking to, otherwise you will be assigning the same value to all values you want to assign the unpacking to, otherwise you will be assigning the
the individual variables! Example: same value to all the individual variables! Example:
.. code-block:: nimrod .. code-block:: nimrod
@ -1386,6 +1386,20 @@ the individual variables! Example:
echo badname echo badname
echo badext echo badext
Tuple unpacking **only** works in ``var`` or ``let`` blocks. The following code
won't compile:
.. code-block:: nimrod
import os
var
path = "usr/local/nimrodc.html"
dir, name, ext = ""
(dir, name, ext) = splitFile(path)
# --> Error: '(dir, name, ext)' cannot be assigned to
Reference and pointer types Reference and pointer types
--------------------------- ---------------------------