From 7113c1e931b3183b3416aec24eb2c13a87619636 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sat, 29 Jun 2019 23:41:25 +0200 Subject: [PATCH] [other] additions to the manual --- doc/manual.rst | 77 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/doc/manual.rst b/doc/manual.rst index cb93541d92..97a5b80530 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -1283,6 +1283,39 @@ Arrays are always bounds checked (statically or at runtime). These checks can be disabled via pragmas or invoking the compiler with the ``--boundChecks:off`` command line switch. +An array constructor can have explicit indexes for readability: + +.. code-block:: nim + + type + Values = enum + valA, valB, valC + + const + lookupTable = [ + valA: "A", + valB: "B", + valC: "C" + ] + +If an index is left out, ``succ(lastIndex)`` is used as the index +value: + +.. code-block:: nim + + type + Values = enum + valA, valB, valC, valD, valE + + const + lookupTable = [ + valA: "A", + "B", + valC: "C", + "D", "e" + ] + + Open arrays ----------- @@ -1667,7 +1700,39 @@ To deal with untraced memory, the procedures ``alloc``, ``dealloc`` and ``realloc`` can be used. The documentation of the system module contains further information. -If a reference points to *nothing*, it has the value ``nil``. + +Nil +--- + +If a reference points to *nothing*, it has the value ``nil``. ``nil`` is also +the default value for all ``ref`` and ``ptr`` types. Dereferencing ``nil`` +is an unrecoverable fatal runtime error. A dereferencing operation ``p[]`` +implies that ``p`` is not nil. This can be exploited by the implementation to +optimize code like: + + +.. code-block:: nim + + p[].field = 3 + if p != nil: + # if p were nil, ``p[]`` would have caused a crash already, + # so we know ``p`` is always not nil here. + action() + +Into: + +.. code-block:: nim + + p[].field = 3 + action() + + +*Note*: This is not comparable to C's "undefined behavior" for +dereferencing NULL pointers. + + +Mixing GC'ed memory with ``ptr`` +-------------------------------- Special care has to be taken if an untraced object contains traced objects like traced references, strings or sequences: in order to free everything properly, @@ -3062,6 +3127,16 @@ Ordinary procs are often preferred over type conversions in Nim: For instance, ``$`` is the ``toString`` operator by convention and ``toFloat`` and ``toInt`` can be used to convert from floating point to integer or vice versa. +A type conversion can also be used to disambiguate overloaded routines: + +.. code-block:: nim + + proc p(x: int) = echo "int" + proc p(x: string) = echo "string" + + let procVar = (proc(x: string))(p) + procVar("a") + Type casts ----------