scope(~this)

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 15 13:36:27 PDT 2017


On Wed, Mar 15, 2017 at 12:34:32PM -0700, Ali Çehreli via Digitalmars-d-learn wrote:
> On 03/15/2017 03:23 AM, Basile B. wrote:
> 
> > you can use a mixin template because they can introduce destructors
> > that are called automatically with the aggregate destrcutor
> >
> > mixin template Foo(T)
> > {
> >     T x;
> >     void foo()
> >     {
> >         alloc(x);
> >     }
> >     ~this() // auto-called by the target aggregate
> >     {
> >         dealloc(x);
> >     }
> > }
> >
> > class bar
> > {
> >     mixin Foo!Stuff;
> > }
> 
> Wow! Is this specified anywhere or have you come across this by chance? :)
[...]

Whoa.  This is a really bizarre unknown (AFAICT) bit of the language (or
at least, dmd's implementation thereof).  I just did a bit of local
testing, and found that while dmd happily chains mixin template dtors
into an aggregate, it behaves erratically if the mixin template declares
this().

It works if there's only a single this() declared either in the class
itself, or a single mixin template.  But if the class declares this()
and the (single) mixin template also declares this(), then the mixin
template's version is ignored and the class-declared this() is run upon
construction.  However, if there are two mixin templates both declaring
this(), the compiler dies with an ICE. :-D


T

-- 
It is widely believed that reinventing the wheel is a waste of time; but I disagree: without wheel reinventers, we would be still be stuck with wooden horse-cart wheels.


More information about the Digitalmars-d-learn mailing list