git

My personal website source code
Log | Files | Refs | Submodules | README | LICENSE

commit 210b064e7f56974a6da4418df8b0037a5631ed30
parent 06eef87dfd925f237de02006ebb7c070440743b1
Author: Luís Ferreira <contact@lsferreira.net>
Date:   Mon, 22 Nov 2021 13:38:43 +0000

posts: Amend 'Zettelkasten #1: Classes in D with betterC'

As suggested by Stanislav Blinov in dlang forum [1]. This also adds a side note
about the presented example.

[1]: https://forum.dlang.org/post/kfzitaarjfgdgexmaglr@forum.dlang.org

Signed-off-by: Luís Ferreira <contact@lsferreira.net>

Diffstat:
Mcontent/posts/zet-1-classes-betterc-d.md | 19+++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/content/posts/zet-1-classes-betterc-d.md b/content/posts/zet-1-classes-betterc-d.md @@ -74,20 +74,20 @@ T alloc(T, Args...)(auto ref Args args) return _t; } (); if(!t) return null; - t.__ctor(args); + import core.lifetime : forward; + t.__ctor(forward!args); return t; } void destroy(T)(ref T t) { - static if (__traits(hasMember, T, "__dtor")) - t.__dtor(); - () @trusted { - import core.memory : pureFree; - pureFree(cast(void*)t); - }(); - t = null; + static if (__traits(hasMember, T, "__xdtor")) + t.__xdtor(); + import core.memory : pureFree; + pureFree(cast(void*)t); + static if (__traits(compiles, { t = null; })) + t = null; } ``` @@ -119,3 +119,6 @@ extern(C) int main() return 0; } ``` + +Please note that I haven't covered every corner case about classes with this +example.