[backport] change some examples in tut1.rst (#18884)

* [backport] change some examples in tut1.rst

* Update doc/tut1.rst

* Update doc/tut1.rst

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
Miran 2021-09-24 11:24:55 +02:00 • committed by GitHub
commit 561b01004c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1643,9 +1643,25 @@ variables! For example:
echo badname echo badname
echo badext echo badext
Tuple unpacking is also supported in for-loops:
.. code-block:: nim
:test: "nim c $1"
let a = [(10, 'a'), (20, 'b'), (30, 'c')]
for (x, c) in a:
echo x
# This will output: 10; 20; 30
# Accessing the index is also possible:
for i, (x, c) in a:
echo i, c
# This will output: 0a; 1b; 2c
Fields of tuples are always public, they don't need to be explicity Fields of tuples are always public, they don't need to be explicity
marked to be exported, unlike for example fields in an object type. marked to be exported, unlike for example fields in an object type.
Reference and pointer types Reference and pointer types
--------------------------- ---------------------------
@ -1675,13 +1691,18 @@ operators perform implicit dereferencing operations for reference types:
Node = ref object Node = ref object
le, ri: Node le, ri: Node
data: int data: int
var
n: Node var n = Node(data: 9)
new(n) echo n.data
n.data = 9
# no need to write n[].data; in fact n[].data is highly discouraged! # no need to write n[].data; in fact n[].data is highly discouraged!
To allocate a new traced object, the built-in procedure `new` must be used. To allocate a new traced object, the built-in procedure `new` can be used:
.. code-block:: nim
var n: Node
new(n)
To deal with untraced memory, the procedures `alloc`, `dealloc` and To deal with untraced memory, the procedures `alloc`, `dealloc` and
`realloc` can be used. The `system <system.html>`_ `realloc` can be used. The `system <system.html>`_
module's documentation contains further details. module's documentation contains further details.
@ -1701,15 +1722,17 @@ Example:
.. code-block:: nim .. code-block:: nim
:test: "nim c $1" :test: "nim c $1"
proc echoItem(x: int) = echo x proc greet(name: string): string =
"Hello, " & name & "!"
proc forEach(action: proc (x: int)) = proc bye(name: string): string =
const "Goodbye, " & name & "."
data = [2, 3, 5, 7, 11]
for d in items(data):
action(d)
forEach(echoItem) proc communicate(greeting: proc (x: string): string, name: string) =
echo greeting(name)
communicate(greet, "John")
communicate(bye, "Mary")
A subtle issue with procedural types is that the calling convention of the A subtle issue with procedural types is that the calling convention of the
procedure influences the type compatibility: procedural types are only compatible procedure influences the type compatibility: procedural types are only compatible