added destructors.rst spec to the build documentation

This commit is contained in:
Araq 2019-08-16 12:14:39 +02:00
commit 1fb9a6d946
3 changed files with 11 additions and 75 deletions

View file

@ -22,6 +22,10 @@ precise wording. This manual is constantly evolving into a proper specification.
**Note**: The experimental features of Nim are **Note**: The experimental features of Nim are
covered `here <manual_experimental.html>`_. covered `here <manual_experimental.html>`_.
**Note**: Assignments, moves and destruction are specified in
the `destructors <destructors.html>`_ document.
This document describes the lexis, the syntax, and the semantics of the Nim language. This document describes the lexis, the syntax, and the semantics of the Nim language.
To learn how to compile Nim programs and generate documentation see To learn how to compile Nim programs and generate documentation see

View file

@ -776,11 +776,12 @@ object inheritance syntax involving the ``of`` keyword:
Type bound operations Type bound operations
===================== =====================
There are 3 operations that are bound to a type: There are 4 operations that are bound to a type:
1. Assignment 1. Assignment
2. Destruction 2. Moves
3. Deep copying for communication between threads 3. Destruction
4. Deep copying for communication between threads
These operations can be *overridden* instead of *overloaded*. This means the These operations can be *overridden* instead of *overloaded*. This means the
implementation is automatically lifted to structured types. For instance if type implementation is automatically lifted to structured types. For instance if type
@ -792,78 +793,8 @@ bound to ``T`` and not to ``ref T``. This also means that one cannot override
``deepCopy`` for both ``ptr T`` and ``ref T`` at the same time; instead a ``deepCopy`` for both ``ptr T`` and ``ref T`` at the same time; instead a
helper distinct or object type has to be used for one pointer type. helper distinct or object type has to be used for one pointer type.
Assignments, moves and destruction are specified in
operator `=` the `destructors <destructors.html>`_ document.
------------
This operator is the assignment operator. Note that in the contexts
``result = expr``, ``parameter = defaultValue`` or for
parameter passing no assignment is performed. For a type ``T`` that has an
overloaded assignment operator ``var v = T()`` is rewritten
to ``var v: T; v = T()``; in other words ``var`` and ``let`` contexts do count
as assignments.
The assignment operator needs to be attached to an object or distinct
type ``T``. Its signature has to be ``(var T, T)``. Example:
.. code-block:: nim
type
Concrete = object
a, b: string
proc `=`(d: var Concrete; src: Concrete) =
shallowCopy(d.a, src.a)
shallowCopy(d.b, src.b)
echo "Concrete '=' called"
var x, y: array[0..2, Concrete]
var cA, cB: Concrete
var cATup, cBTup: tuple[x: int, ha: Concrete]
x = y
cA = cB
cATup = cBTup
destructors
-----------
A destructor must have a single parameter with a concrete type (the name of a
generic type is allowed too). The name of the destructor has to be ``=destroy``.
``=destroy(v)`` will be automatically invoked for every local stack
variable ``v`` that goes out of scope.
If a structured type features a field with destructable type and
the user has not provided an explicit implementation, a destructor for the
structured type will be automatically generated. Calls to any base class
destructors in both user-defined and generated destructors will be inserted.
A destructor is attached to the type it destructs.
.. code-block:: nim
type
MyObj = object
x, y: int
p: pointer
proc `=destroy`(o: var MyObj) =
if o.p != nil: dealloc o.p
proc open: MyObj =
result = MyObj(x: 1, y: 2, p: alloc(3))
proc work(o: MyObj) =
echo o.x
# No destructor invoked here for 'o' as 'o' is a parameter.
proc main() =
# destructor automatically invoked at the end of the scope:
var x = open()
# valid: pass 'x' to some other proc:
work(x)
deepCopy deepCopy

View file

@ -97,6 +97,7 @@ doc/apis.rst
doc/lib.rst doc/lib.rst
doc/manual.rst doc/manual.rst
doc/manual_experimental.rst doc/manual_experimental.rst
doc/destructors.rst
doc/tut1.rst doc/tut1.rst
doc/tut2.rst doc/tut2.rst
doc/tut3.rst doc/tut3.rst