On Tuesday, 20 March 2018 at 21:27:53 UTC, 12345swordy wrote:
This is very important to me as I am very interested in using
the language for game development.
Yes I know that it's marked as "Duplicated", but I strongly
disagree as it is different enough to consider is own issue.
Alex
Here's something you can study. It's the only solution i've found
so far and it's implemented in my user library called "IZ"
(https://github.com/BBasile/iz). You can run the stuff simply by
passing it as a file name to DUB (aka single file package):
```
/+dub.sdl:
name "destruct_demo"
dependency "iz" version="*"
+/
module runnable;
import std.stdio, iz.memory, iz.enumset;
enum Foo {f1,f2,f3}
alias SetOfFoo = EnumSet!(Foo,Set8);
SetOfFoo foos;
class Foo1
{
this() @nogc {foos = 0;}
mixin inheritedDtor;
~this() @nogc
{
foos.include(Foo.f1);
callInheritedDtor();
}
}
class Foo2 : Foo1
{
mixin inheritedDtor;
~this() @nogc
{
foos.include(Foo.f2);
callInheritedDtor();
}
}
class Foo3 : Foo2
{
mixin inheritedDtor;
~this() @nogc
{
foos.include(Foo.f3);
callInheritedDtor();
}
}
void testTrustedStaticType() @nogc
{
Foo1 f1 = construct!Foo3;
assert(foos.none);
destruct(f1);
// destruct trusts f1 static type, which is wrong
// with a dynamic destruct() we should get [Foo.f1, Foo.f2,
Foo.f3]
assert(foos == SetOfFoo(Foo.f1));
Foo3 f3 = construct!Foo3;
assert(foos.none);
destruct(f3);
// destruct trusts f3 static type, which is right
assert(foos == SetOfFoo(Foo.f1, Foo.f2, Foo.f3));
}
void testDynamicType() // cant be @nogc
{
Foo1 f1 = construct!Foo3;
assert(foos.none);
destruct(cast(Object) f1);
// an Object is passed to "destruct" which means "detect
dynamic type"
assert(foos == [Foo.f1, Foo.f2, Foo.f3]);
Foo1 f2 = construct!Foo3;
assert(foos.none);
destruct(cast(Object) f2);
// an Object is passed to "destruct" which means "detect
dynamic type"
assert(foos == [Foo.f1, Foo.f2, Foo.f3]);
}
void main()
{
testTrustedStaticType();
testDynamicType();
}
```
Important points:
- not compatible with D runtime
- use mixins to overcome the fact that destructor are not virtual
(it's a lie)
- to be @nogc, the static type has to be known, really.