On 10/2/22 10:28, data pulverizer wrote:
> On Sunday, 2 October 2022 at 17:19:55 UTC, data pulverizer wrote:
>> Any reason why this could be?
>

What I noticed first in your original code was that it would be considered buggy because it was not considering copying. Every struct that does something in its destructor should either have post-blit (or copy constructor) defined or simpler, disallow copying altogether.

That's what I did here:

  https://github.com/acehreli/alid/blob/main/cached/alid/cached.d#L178

    @disable this(this);

I think disabling copy constructor was unnecessary but I did that as well:

    @disable this(ref const(typeof(this)));

The issue remains and bothers me as well. I think writeln copies objects because D disallows references to rvalue. We couldn't print rvalues if writeln insisted on 'ref'. Or, rvalues would be copied anyway if we used 'auto ref'. Hence the status quo...

> Sorry I'll need to implement all the overloaded copy constructors and
> see if that fixes it.

The best solution I know is to disable copying and printing not the object but an explicit string representation of it:

Added:

    @disable this(this);

Added (there are better ways of doing the same e.g. using a 'sink' parameter):

    string toString() const {
        import std.format : format;
        return format!"id: %s"(id);
    }

Called toString:

    writeln("MyObject: ", obj.toString);

Ali


Reply via email to