mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 18:02:05 +00:00
Merge pull request #4480 from apahl/devel
explain difference between Table and TableRef in tables.nim
This commit is contained in:
@@ -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 = {1: "one", 2: "two"}.toTable # 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 = {1: "one", 2: "two"}.newTable # 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
|
||||
|
||||
Reference in New Issue
Block a user