[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:
parent
90a2b5afd8
commit
561b01004c
1 changed files with 35 additions and 12 deletions
47
doc/tut1.rst
47
doc/tut1.rst
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue