mirror of
https://github.com/nim-lang/Nim.git
synced 2026-09-19 03:24:53 +00:00
@@ -5,7 +5,9 @@ destroy
|
||||
5
|
||||
123
|
||||
destroy Foo: 123
|
||||
destroy Foo: 5'''
|
||||
destroy Foo: 5
|
||||
(x1: (val: ...))
|
||||
destroy'''
|
||||
joinable: false
|
||||
"""
|
||||
|
||||
@@ -49,3 +51,45 @@ proc main =
|
||||
test(toFooPtr(123))
|
||||
|
||||
main()
|
||||
|
||||
# bug #11517
|
||||
type
|
||||
UniquePtr*[T] = object
|
||||
val: ptr T
|
||||
|
||||
proc `=destroy`*[T](p: var UniquePtr[T]) =
|
||||
mixin `=destroy`
|
||||
echo "destroy"
|
||||
if p.val != nil:
|
||||
`=destroy`(p.val[])
|
||||
dealloc(p.val)
|
||||
p.val = nil
|
||||
|
||||
proc `=`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.error.}
|
||||
|
||||
proc `=sink`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.inline.} =
|
||||
if dest.val != src.val:
|
||||
if dest.val != nil:
|
||||
`=destroy`(dest)
|
||||
dest.val = src.val
|
||||
|
||||
proc newUniquePtr*[T](val: sink T): UniquePtr[T] =
|
||||
result.val = create(T)
|
||||
result.val[] = val
|
||||
|
||||
#-------------------------------------------------------------
|
||||
|
||||
type
|
||||
MyObject = object of RootObj
|
||||
x1: UniquePtr[int]
|
||||
|
||||
MyObject2 = object of MyObject
|
||||
|
||||
proc newObj2(x:int, y: float): MyObject2 =
|
||||
MyObject2(x1: newUniquePtr(x))
|
||||
|
||||
proc test =
|
||||
let obj2 = newObj2(1, 1.0)
|
||||
echo obj2
|
||||
|
||||
test()
|
||||
|
||||
Reference in New Issue
Block a user