Is it in specifications?
type Type[T] = object
id: Natural
x: T
echo Type[int] isnot Type[float]
proc `=destroy`[T](x: var Type[T]) =
echo "DESTROY ", typeof x, " id:", x.id
var A {.used.} = Type[int](id: 1)
var B {.used.} = Type[float](id: 0)
output:
true
DESTROY Type[system.int] id:0
DESTROY Type[system.int] id:1
In B, I want to call a destructor of type float.
type Type[T] = object
id: Natural
x: T
proc `=destroy`(x: var Type[int]) =
echo "DESTROY ", typeof x, " id:", x.id
proc `=destroy`(x: var Type[float]) =
echo "DESTROY ", typeof x, " id:", x.id
var A {.used.} = Type[int](id: 1)
var B {.used.} = Type[float](id: 0)
Above works fine, but it's a bit tedious. There is another problem: if x: T of above is no longer defined, either one destructor cannot be defined.
If this is the spec, is there a workaround? I want to change the destructor process for each Type content.
Keep in mind che imho the ARC/ORC are basically broken at the moment and probably that's why refc it's still the default GC.
I experienced compiler crashes (internal error) https://github.com/nim-lang/Nim/issues/19401
Finalizers not called with forward declarations: https://github.com/nim-lang/Nim/issues/19402
WARNING: RANT MODE ON
Destructors and move semantics documention is completely unusuable and lacking. I would write the missing bits my self if i got it but it's incromprensible and lacking. It's not clearly stated how it plays with both inheritance and ref types and lacks of parallelism with how it's implemented in other languages. Given that i undestand how it works in C++ and also in other languages i would say that "probably" (and i remark probably) there's something weird going on. Honestly the last working releases of Nim was 1.2. Both 1.4 and 1.6 are just work in progress releases with most of the new features left in a somewhat working state (arc/orc, threading, concepts) but never completed. I doubt i would use nim for any serious or even hobby projects
Thanks for the reply. It certainly worked correctly.
Actually, there is another problem I would like to solve. I want to make sure that =destroy correctly recognizes the Type in the following source code:
type TypeKind = enum
A = "A"
B = "B"
type Type[T: static TypeKind] = object
id: Natural
proc `=destroy`[T](x: var Type[T]) =
echo "DESTROY ", typeof x, " id:", x.id
var a {.used.} = Type[A](id: 1)
var b {.used.} = Type[B](id: 0)
# OUTPUT:
# DESTROY Type[A] id:0
# DESTROY Type[A] id:1
But now it does not work correctly. Compared to the example in the first post, the definition of x: T is missing from Type. I suspect that this makes the contents of Type[A] and Type[B] indistinguishable.
It would work if I implemented =destroy individually, as in the second example in the first post, but I'd like to avoid that as much as possible. Is there a better way to do this?
In general global variables have the same lifetime as the program so they are always alive.
I wasn't even aware that they triggered destructors.