improvements for destructors
This commit is contained in:
parent
d57fe6c904
commit
bb3f648bd2
6 changed files with 91 additions and 35 deletions
21
tests/reject/tdestructor.nim
Normal file
21
tests/reject/tdestructor.nim
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
discard """
|
||||
line: 20
|
||||
errormsg: " usage of a type with a destructor in a non destructible context"
|
||||
"""
|
||||
|
||||
type
|
||||
TMyObj = object
|
||||
x, y: int
|
||||
p: pointer
|
||||
|
||||
proc destruct(o: var TMyObj) {.destructor.} =
|
||||
if o.p != nil: dealloc o.p
|
||||
|
||||
proc open: TMyObj =
|
||||
result = TMyObj(x: 1, y: 2, p: alloc(3))
|
||||
|
||||
|
||||
proc `$`(x: TMyObj): string = $x.y
|
||||
|
||||
echo open()
|
||||
|
||||
26
tests/run/tdestructor.nim
Normal file
26
tests/run/tdestructor.nim
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
discard """
|
||||
output: '''some text
|
||||
Destructor called!'''
|
||||
"""
|
||||
|
||||
type
|
||||
TMyObj = object
|
||||
x, y: int
|
||||
p: pointer
|
||||
|
||||
proc destruct(o: var TMyObj) {.destructor.} =
|
||||
if o.p != nil: dealloc o.p
|
||||
echo "Destructor called!"
|
||||
|
||||
proc open: TMyObj =
|
||||
# allow for superfluous ()
|
||||
result = (TMyObj(x: 1, y: 2, p: alloc(3)))
|
||||
|
||||
|
||||
proc `$`(x: TMyObj): string = $x.y
|
||||
|
||||
proc main() =
|
||||
var x = open()
|
||||
echo "some text"
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue