DIP44: scope(class) and scope(struct)
H. S. Teoh
hsteoh at quickfur.ath.cx
Sat Aug 24 13:27:39 PDT 2013
On Sat, Aug 24, 2013 at 01:09:44PM -0700, H. S. Teoh wrote:
[...]
> On Sat, Aug 24, 2013 at 12:27:37PM -0700, Walter Bright wrote:
[...]
> > 4. The worst issue is the DIP assumes there is only one constructor,
> > from which the destructor is inferred. What if there is more than
> > one constructor?
>
> This is not a problem. If there is more than one constructor, then
> only those scope(this) statements in the ctor that were actually
> encountered will trigger when the object reaches the end of its
> lifetime. You already have to do this anyway, since if the ctor
> throws an Exception before completely constructing the object, only
> those scope(this) statements that have been encountered up to that
> point will be triggered, not all of them. Otherwise, you'd still have
> the partially-initialized object problem.
[...]
Argh, that was poorly worded. What I mean is this:
struct S {
this(int) {
scope(this) writeln("A");
}
this(float) {
scope(this) writeln("B");
}
}
void fun1() {
auto s = S(1);
} // prints "A"
void fun2() {
auto s = S(1.0);
} // prints "B"
T
--
Dogs have owners ... cats have staff. -- Krista Casada
More information about the Digitalmars-d
mailing list