diff options
author | Axel Pahl <axelpahl@gmx.de> | 2016-07-12 09:43:31 +0200 |
---|---|---|
committer | Axel Pahl <axelpahl@gmx.de> | 2016-07-12 09:43:31 +0200 |
commit | 8d4614b5ab4fbf8ccb34875c2c2f1e7dcddcc2ee (patch) | |
tree | 00e2a225e4531a864f7ca221a02b2f332bb7f52b /lib/pure | |
parent | 84c3830c6bb7b73a526dff81946ba53ebd4f0edb (diff) | |
download | Nim-8d4614b5ab4fbf8ccb34875c2c2f1e7dcddcc2ee.tar.gz |
explain difference between Table and TableRef in tables.nim
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/collections/tables.nim | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index e454a43cb..9feea3746 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -16,6 +16,39 @@ ## semantics, this means that ``=`` performs a copy of the hash table. ## For **reference** semantics use the ``Ref`` variant: ``TableRef``, ## ``OrderedTableRef``, ``CountTableRef``. +## To give an example, when `a` is a Table, then `var b = a` gives `b` +## as a new independent table. b is initialised with the contents of `a`. +## Changing `b` does not affect `a` and vice versa: +## +## .. code-block:: +## import tables +## +## var +## a = toTable([(1, "one"), (2, "two")]) # creates a Table +## b = a +## +## echo a, b # output: {1: one, 2: two}{1: one, 2: two} +## +## b[3] = "three" +## echo a, b # output: {1: one, 2: two}{1: one, 2: two, 3: three} +## echo a == b # output: false +## +## On the other hand, when `a` is a TableRef instead, then changes to `b` also affect `a`. +## Both `a` and `b` reference the same data structure: +## +## .. code-block:: +## import tables +## +## var +## a = newTable([(1, "one"), (2, "two")]) # creates a TableRef +## b = a +## +## echo a, b # output: {1: one, 2: two}{1: one, 2: two} +## +## b[3] = "three" +## echo a, b # output: {1: one, 2: two, 3: three}{1: one, 2: two, 3: three} +## echo a == b # output: true +## ## ## If you are using simple standard types like ``int`` or ``string`` for the ## keys of the table you won't have any problems, but as soon as you try to use |