Disallow destroy(structPtr)?
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Feb 20 12:30:04 PST 2015
On 2/20/15 1:05 PM, Nick Treleaven wrote:
> Hi,
>
> The following code is supposed to destroy the struct instance:
>
> import std.stdio;
> struct S{
> ~this(){"destruct".writeln;}
> }
> auto p = new S;
> destroy(p);
> "end".writeln;
>
> It works correctly if I use destroy(*p), but the above code could
> perhaps be statically rejected by object.destroy to help prevent bugs.
> Currently, the pointer p is set to null without calling the destructor
> (with recent dmd the destructor is called, but only after "end" is
> printed).
>
> Here is the destroy overload:
>
> void destroy(T)(ref T obj)
> if (!is(T == struct) && !is(T == interface) && !is(T == class) &&
> !_isStaticArray!T)
> {
> obj = T.init;
> }
I'm beginning to think this is the right thing to do. It confuses so
many people, and setting a pointer/class reference/array to null is easy
enough without needing a special function to do it. In other words, if
you are using destroy, you aren't just trying to nullify a pointer. You
want to destroy what the pointer represents.
The only problem is, how does this affect existing code?
-Steve
More information about the Digitalmars-d-learn
mailing list