Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Improve destroy's documentation #2054

Merged
merged 1 commit into from
Jan 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/object.d
Original file line number Diff line number Diff line change
Expand Up @@ -2878,12 +2878,12 @@ unittest
assert(postblitRecurseOrder == order);
}

/++
Destroys the given object and puts it in an invalid state. It's used to
_destroy an object so that any cleanup which its destructor or finalizer
does is done and so that it no longer references any other objects. It does
$(I not) initiate a GC cycle or free any GC memory.
+/
/********
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need to define a best practice for docstrings.
Generally I'm a fan of using only as many * / + as strictly necessary:

/++

+/

or

/**

*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Not a fan of lots of stars or stars at the beginning of each comment line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following @andralex's prior suggestion here: dlang/dmd#6835 (comment)

Let's use https://github.com/dlang/phobos/blob/master/std/string.d#L2254-L2267 for new code, thanks.

Anyway, if it's important, please make a PR to the style guide with preferred dos and don'ts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Destroys the given object and sets it back to its initial state. It's used to
_destroy an object, calling its destructor or finalizer so it no longer
references any other objects. It does $(I not) initiate a GC cycle or free
any GC memory.
*/
void destroy(T)(T obj) if (is(T == class))
{
rt_finalize(cast(void*)obj);
Expand All @@ -2895,6 +2895,38 @@ void destroy(T)(T obj) if (is(T == interface))
destroy(cast(Object)obj);
}

/// Reference type demonstration
unittest
{
class C
{
static int dtorCount;

string s = "S";
~this() { dtorCount++; }
}

C c = new C();
assert(c.dtorCount == 0); // destructor not yet called
assert(c.s == "S"); // initial state `c.s` is `"S"`
c.s = "T";
assert(c.s == "T"); // `c.s` is `"T"`
destroy(c);
assert(c.dtorCount == 1); // `c`'s destructor was called
assert(c.s == "S"); // `c.s` is back to its inital state, `"S"`
}

/// Value type demonstration
unittest
{
int i;
assert(i == 0); // `i`'s initial state is `0`
i = 1;
assert(i == 1); // `i` changed to `1`
destroy(i);
assert(i == 0); // `i` is back to its initial state `0`
}

unittest
{
interface I { }
Expand Down