mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
better alternative to 'override'
This commit is contained in:
@@ -56,10 +56,9 @@ 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``
|
||||
and it need to be annotated with the ``override`` pragma.
|
||||
generic type is allowed too). The name of the destructor has to be ``=destroy``.
|
||||
|
||||
``destroy(v)`` will be automatically invoked for every local stack
|
||||
``=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
|
||||
@@ -76,7 +75,7 @@ can then only be used in *destructible contexts* and as parameters:
|
||||
x, y: int
|
||||
p: pointer
|
||||
|
||||
proc destroy(o: var MyObj) {.override.} =
|
||||
proc `=destroy`(o: var MyObj) =
|
||||
if o.p != nil: dealloc o.p
|
||||
|
||||
proc open: MyObj =
|
||||
@@ -108,7 +107,7 @@ be destructed at its scope exit. Later versions of the language will improve
|
||||
the support of destructors.
|
||||
|
||||
Be aware that destructors are not called for objects allocated with ``new``.
|
||||
This may change in future versions of language, but for now the ``finalizer``
|
||||
This may change in future versions of language, but for now the `finalizer`:idx:
|
||||
parameter to ``new`` has to be used.
|
||||
|
||||
**Note**: Destructors are still experimental and the spec might change
|
||||
@@ -118,7 +117,7 @@ significantly in order to incorporate an escape analysis.
|
||||
deepCopy
|
||||
--------
|
||||
|
||||
``deepCopy`` is a builtin that is invoked whenever data is passed to
|
||||
``=deepCopy`` is a builtin that is invoked whenever data is passed to
|
||||
a ``spawn``'ed proc to ensure memory safety. The programmer can override its
|
||||
behaviour for a specific ``ref`` or ``ptr`` type ``T``. (Later versions of the
|
||||
language may weaken this restriction.)
|
||||
@@ -126,7 +125,7 @@ language may weaken this restriction.)
|
||||
The signature has to be:
|
||||
|
||||
.. code-block:: nim
|
||||
proc deepCopy(x: T): T {.override.}
|
||||
proc `=deepCopy`(x: T): T
|
||||
|
||||
This mechanism is used by most data structures that support shared memory like
|
||||
channels to implement thread safe automatic memory management.
|
||||
|
||||
@@ -40,7 +40,7 @@ type
|
||||
x: A
|
||||
y: B
|
||||
z: C
|
||||
|
||||
|
||||
TObjKind = enum A, B, C, D
|
||||
|
||||
TCaseObj = object
|
||||
@@ -57,14 +57,14 @@ type
|
||||
q: TMyGeneric3[TMyObj, int, int]
|
||||
r: string
|
||||
|
||||
proc destroy(o: var TMyObj) {.override.} =
|
||||
proc `=destroy`(o: var TMyObj) =
|
||||
if o.p != nil: dealloc o.p
|
||||
echo "myobj destroyed"
|
||||
|
||||
proc destroy(o: var TMyGeneric1) {.override.} =
|
||||
proc `=destroy`(o: var TMyGeneric1) =
|
||||
echo "mygeneric1 destroyed"
|
||||
|
||||
proc destroy[A, B](o: var TMyGeneric2[A, B]) {.override.} =
|
||||
proc `=destroy`[A, B](o: var TMyGeneric2[A, B]) =
|
||||
echo "mygeneric2 destroyed"
|
||||
|
||||
proc open: TMyObj =
|
||||
@@ -83,12 +83,12 @@ proc mygeneric1() =
|
||||
|
||||
proc mygeneric2[T](val: T) =
|
||||
var a = open()
|
||||
|
||||
|
||||
var b = TMyGeneric2[int, T](x: 10, y: val)
|
||||
echo "mygeneric2 constructed"
|
||||
|
||||
var c = TMyGeneric3[int, int, string](x: 10, y: 20, z: "test")
|
||||
|
||||
|
||||
proc mygeneric3 =
|
||||
var x = TMyGeneric3[int, string, TMyGeneric1[int]](
|
||||
x: 10, y: "test", z: TMyGeneric1[int](x: 10))
|
||||
@@ -111,11 +111,11 @@ proc caseobj =
|
||||
block:
|
||||
echo "----"
|
||||
var o1 = TCaseObj(kind: A, x: TMyGeneric1[int](x: 10))
|
||||
|
||||
|
||||
block:
|
||||
echo "----"
|
||||
var o2 = TCaseObj(kind: B, y: open())
|
||||
|
||||
|
||||
block:
|
||||
echo "----"
|
||||
var o3 = TCaseObj(kind: D, innerKind: B, r: "test",
|
||||
|
||||
@@ -5,14 +5,14 @@ discard """
|
||||
|
||||
{.experimental.}
|
||||
|
||||
type
|
||||
type
|
||||
TMyObj = object
|
||||
x, y: int
|
||||
p: pointer
|
||||
|
||||
proc destroy(o: var TMyObj) {.override.} =
|
||||
|
||||
proc `=destroy`(o: var TMyObj) =
|
||||
if o.p != nil: dealloc o.p
|
||||
|
||||
|
||||
proc open: TMyObj =
|
||||
result = TMyObj(x: 1, y: 2, p: alloc(3))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user