Classes in D with betterC

Luís Ferreira contact at lsferreira.net
Mon Nov 22 12:57:27 UTC 2021


On Mon, 2021-11-22 at 03:36 +0000, Stanislav Blinov via Digitalmars-d-
announce wrote:
> On Sunday, 21 November 2021 at 20:30:51 UTC, Luís Ferreira wrote:
> 
> > I decided to have a simple approach just to show that it is 
> > easier now to do that without workarounds, and by workarounds, 
> > I mean using scope to fetch that info and use a fake dtor, or 
> > even before scope exists, the usage of runtime-less D call with 
> > betterC + objcopy, described 
> > [here](
> > https://theartofmachinery.com/2018/05/27/cpp_classes_in_betterc.
> > html).
> 
> I get that the news part is all about `__traits(initSymbol)`, 
> which in this case is about instantiating a class without 
> druntime.

Yes and this is by far harder than the past/previous approach, as you
refered wrongly, "this has been possible for ages".

> But that's just one line in the whole example. And the rest of 
> the code? Construction may either fail altogether or cause 
> unnecessary copies.

I acknowledge that is a thing to consider, when doing generic code. I'm
going to be honest and say that I did't though about those cases at the
time of writing. The particular case is not wrong however, it is just
limitative to types without such qualifiers. If I went that far,
probably there is a number of things wrong. What if I want an
`immutable` version of T? I simply, didn't thought about it, mostly
because the point of that zettelkasten was to show
`__traits(initSymbol)` off.

> Calling __dtor won't call destructors of base 
> classes or derived classes. There may not even be a __dtor yet 
> destruction may still be required through __xdtor, which still 
> would not destruct base or derived classes.

This is an undocumented feature that I didn't know about. I'm going to
do a specification PR to amend it.

> Deallocation can't be 
> @trusted since you don't have a facility to prove that no other 
> references exist.

Yes, that is totally wrong and should be fixed, alhtough trusting
pureMalloc is fine.

> If anything, the code shows how NOT to use 
> classes, contrary to what your title or preface say ;)
> Even simple, an example should at least be correct, don't you 
> think?

I think you unproportionally scaled your argument -- the article is not
a complete how-to guide in classes -- althouh I agree that a simple
example like the one presented should be correct.

Thanks for your input! I'm going to fix the issues you presented.

-- 
Sincerely,
Luís Ferreira @ lsferreira.net

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-announce/attachments/20211122/44ed49e8/attachment.sig>


More information about the Digitalmars-d-announce mailing list