DIP44: scope(class) and scope(struct)
H. S. Teoh
hsteoh at quickfur.ath.cx
Sun Aug 25 00:14:18 PDT 2013
On Sat, Aug 24, 2013 at 11:30:26PM -0700, Walter Bright wrote:
[...]
> Your example, again, is of an auto-generated dtor. But you said
> earlier that wasn't the point.
>
> Without the auto-generated dtor, it is just scope(failure), which is
> already a D feature.
>
> I don't get it.
It's a way to avoid code duplication in the dtor. If you just had
scope(failure), you have to factor out a __cleanup method in order to do
cleanup on both a failed ctor call or a dtor call, as I did in the
example.
I don't know if it will clarify things, but if you already have a dtor,
then scope(this) just adds to it:
class C {
this() {
scope(this) writeln("A");
scope(this) writeln("B");
}
~this() {
writeln("In dtor");
}
}
This would get lowered to the equivalent of:
class C {
this() {
scope(failure) __cleanup();
__cleanups ~= { writeln("A"); };
__cleanups ~= { writeln("B"); };
}
void delegate()[] __cleanups;
void __cleanup() {
foreach_reverse (f; __cleanups)
f();
}
~this() {
writeln("In dtor");
__cleanup();
}
}
T
--
Skill without imagination is craftsmanship and gives us many useful
objects such as wickerwork picnic baskets. Imagination without skill
gives us modern art. -- Tom Stoppard
More information about the Digitalmars-d
mailing list