better alternative to 'override'
This commit is contained in:
parent
82f8948a10
commit
73add468b7
3 changed files with 18 additions and 19 deletions
|
|
@ -56,10 +56,9 @@ destructors
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
A destructor must have a single parameter with a concrete type (the name of a
|
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``
|
generic type is allowed too). The name of the destructor has to be ``=destroy``.
|
||||||
and it need to be annotated with the ``override`` pragma.
|
|
||||||
|
|
||||||
``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.
|
variable ``v`` that goes out of scope.
|
||||||
|
|
||||||
If a structured type features a field with destructable type and
|
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
|
x, y: int
|
||||||
p: pointer
|
p: pointer
|
||||||
|
|
||||||
proc destroy(o: var MyObj) {.override.} =
|
proc `=destroy`(o: var MyObj) =
|
||||||
if o.p != nil: dealloc o.p
|
if o.p != nil: dealloc o.p
|
||||||
|
|
||||||
proc open: MyObj =
|
proc open: MyObj =
|
||||||
|
|
@ -108,7 +107,7 @@ be destructed at its scope exit. Later versions of the language will improve
|
||||||
the support of destructors.
|
the support of destructors.
|
||||||
|
|
||||||
Be aware that destructors are not called for objects allocated with ``new``.
|
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.
|
parameter to ``new`` has to be used.
|
||||||
|
|
||||||
**Note**: Destructors are still experimental and the spec might change
|
**Note**: Destructors are still experimental and the spec might change
|
||||||
|
|
@ -118,7 +117,7 @@ significantly in order to incorporate an escape analysis.
|
||||||
deepCopy
|
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
|
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
|
behaviour for a specific ``ref`` or ``ptr`` type ``T``. (Later versions of the
|
||||||
language may weaken this restriction.)
|
language may weaken this restriction.)
|
||||||
|
|
@ -126,7 +125,7 @@ language may weaken this restriction.)
|
||||||
The signature has to be:
|
The signature has to be:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. 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
|
This mechanism is used by most data structures that support shared memory like
|
||||||
channels to implement thread safe automatic memory management.
|
channels to implement thread safe automatic memory management.
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ type
|
||||||
x: A
|
x: A
|
||||||
y: B
|
y: B
|
||||||
z: C
|
z: C
|
||||||
|
|
||||||
TObjKind = enum A, B, C, D
|
TObjKind = enum A, B, C, D
|
||||||
|
|
||||||
TCaseObj = object
|
TCaseObj = object
|
||||||
|
|
@ -57,14 +57,14 @@ type
|
||||||
q: TMyGeneric3[TMyObj, int, int]
|
q: TMyGeneric3[TMyObj, int, int]
|
||||||
r: string
|
r: string
|
||||||
|
|
||||||
proc destroy(o: var TMyObj) {.override.} =
|
proc `=destroy`(o: var TMyObj) =
|
||||||
if o.p != nil: dealloc o.p
|
if o.p != nil: dealloc o.p
|
||||||
echo "myobj destroyed"
|
echo "myobj destroyed"
|
||||||
|
|
||||||
proc destroy(o: var TMyGeneric1) {.override.} =
|
proc `=destroy`(o: var TMyGeneric1) =
|
||||||
echo "mygeneric1 destroyed"
|
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"
|
echo "mygeneric2 destroyed"
|
||||||
|
|
||||||
proc open: TMyObj =
|
proc open: TMyObj =
|
||||||
|
|
@ -83,12 +83,12 @@ proc mygeneric1() =
|
||||||
|
|
||||||
proc mygeneric2[T](val: T) =
|
proc mygeneric2[T](val: T) =
|
||||||
var a = open()
|
var a = open()
|
||||||
|
|
||||||
var b = TMyGeneric2[int, T](x: 10, y: val)
|
var b = TMyGeneric2[int, T](x: 10, y: val)
|
||||||
echo "mygeneric2 constructed"
|
echo "mygeneric2 constructed"
|
||||||
|
|
||||||
var c = TMyGeneric3[int, int, string](x: 10, y: 20, z: "test")
|
var c = TMyGeneric3[int, int, string](x: 10, y: 20, z: "test")
|
||||||
|
|
||||||
proc mygeneric3 =
|
proc mygeneric3 =
|
||||||
var x = TMyGeneric3[int, string, TMyGeneric1[int]](
|
var x = TMyGeneric3[int, string, TMyGeneric1[int]](
|
||||||
x: 10, y: "test", z: TMyGeneric1[int](x: 10))
|
x: 10, y: "test", z: TMyGeneric1[int](x: 10))
|
||||||
|
|
@ -111,11 +111,11 @@ proc caseobj =
|
||||||
block:
|
block:
|
||||||
echo "----"
|
echo "----"
|
||||||
var o1 = TCaseObj(kind: A, x: TMyGeneric1[int](x: 10))
|
var o1 = TCaseObj(kind: A, x: TMyGeneric1[int](x: 10))
|
||||||
|
|
||||||
block:
|
block:
|
||||||
echo "----"
|
echo "----"
|
||||||
var o2 = TCaseObj(kind: B, y: open())
|
var o2 = TCaseObj(kind: B, y: open())
|
||||||
|
|
||||||
block:
|
block:
|
||||||
echo "----"
|
echo "----"
|
||||||
var o3 = TCaseObj(kind: D, innerKind: B, r: "test",
|
var o3 = TCaseObj(kind: D, innerKind: B, r: "test",
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,14 @@ discard """
|
||||||
|
|
||||||
{.experimental.}
|
{.experimental.}
|
||||||
|
|
||||||
type
|
type
|
||||||
TMyObj = object
|
TMyObj = object
|
||||||
x, y: int
|
x, y: int
|
||||||
p: pointer
|
p: pointer
|
||||||
|
|
||||||
proc destroy(o: var TMyObj) {.override.} =
|
proc `=destroy`(o: var TMyObj) =
|
||||||
if o.p != nil: dealloc o.p
|
if o.p != nil: dealloc o.p
|
||||||
|
|
||||||
proc open: TMyObj =
|
proc open: TMyObj =
|
||||||
result = TMyObj(x: 1, y: 2, p: alloc(3))
|
result = TMyObj(x: 1, y: 2, p: alloc(3))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue