mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-21 23:05:27 +00:00
improvements for destructors
This commit is contained in:
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()
|
||||
Reference in New Issue
Block a user