diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2018-04-29 08:14:00 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-04-29 08:14:00 +0200 |
commit | 5237ef4f5280fef8df630f265cc67869d013c0ef (patch) | |
tree | cfd264df55cf397b32174539833b736704b795c0 /doc/tut1.rst | |
parent | fb15a265c5ec1bb6081abc25fc093c1223b8faf3 (diff) | |
download | Nim-5237ef4f5280fef8df630f265cc67869d013c0ef.tar.gz |
update the documentation about the new strings/seqs behaviours
Diffstat (limited to 'doc/tut1.rst')
-rw-r--r-- | doc/tut1.rst | 18 |
1 files changed, 4 insertions, 14 deletions
diff --git a/doc/tut1.rst b/doc/tut1.rst index cbfef183e..2dc502fab 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -960,11 +960,7 @@ enforced. For example, when reading strings from binary files, they are merely a sequence of bytes. The index operation ``s[i]`` means the i-th *char* of ``s``, not the i-th *unichar*. -String variables are initialized with a special value, called ``nil``. However, -most string operations cannot deal with ``nil`` (leading to an exception being -raised) for performance reasons. It is best to use empty strings ``""`` -rather than ``nil`` as the *empty* value. But ``""`` often creates a string -object on the heap, so there is a trade-off to be made here. +String variables are initialized with the empty strings ``""``. Integers @@ -1309,11 +1305,7 @@ Example: x: seq[int] # a reference to a sequence of integers x = @[1, 2, 3, 4, 5, 6] # the @ turns the array into a sequence allocated on the heap -Sequence variables are initialized with ``nil``. However, most sequence -operations cannot deal with ``nil`` (leading to an exception being -raised) for performance reasons. Thus one should use empty sequences ``@[]`` -rather than ``nil`` as the *empty* value. But ``@[]`` creates a sequence -object on the heap, so there is a trade-off to be made here. +Sequence variables are initialized with ``@[]``. The ``for`` statement can be used with one or two variables when used with a sequence. When you use the one variable form, the variable will hold the value @@ -1355,11 +1347,9 @@ type does not matter. .. code-block:: nim :test: "nim c $1" var - fruits: seq[string] # reference to a sequence of strings that is initialized with 'nil' + fruits: seq[string] # reference to a sequence of strings that is initialized with '@[]' capitals: array[3, string] # array of strings with a fixed size - fruits = @[] # creates an empty sequence on the heap that will be referenced by 'fruits' - capitals = ["New York", "London", "Berlin"] # array 'capitals' allows assignment of only three elements fruits.add("Banana") # sequence 'fruits' is dynamically expandable during runtime fruits.add("Mango") @@ -1691,7 +1681,7 @@ rules apply: write(stdout, x(3)) # no error: A.x is called write(stdout, x("")) # no error: B.x is called - proc x*(a: int): string = nil + proc x*(a: int): string = discard write(stdout, x(3)) # ambiguous: which `x` is to call? |