March 15, 2017
On Wednesday, 15 March 2017 at 12:56:10 UTC, Inquie wrote:
> If it is trivial, can you write up a quick solution. I don't see how to accomplish it easily.

Here it is, source and output:
https://dpaste.dzfl.pl/bbf162529c6c


March 15, 2017
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? :)

Ali

March 15, 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.
March 15, 2017
On Wednesday, 15 March 2017 at 20:36:27 UTC, H. S. Teoh wrote:
> 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.

That's normal mixin template behavior: if the name of a mixed in object already exists in the instantiation scope, the existing one is preferred. This allows you to "override" mixin functions and is quite useful. You can also bring in the others via alias.

See my tip here for a related factoid:
http://arsdnet.net/this-week-in-d/2016-feb-07.html


That's what I *thought* was going on with the destructor too... but I confirmed both do in fact exist and seem to be merged by the compiler. I knew it did that with static dtors, but the spec says "a class can have only one destructor and it is always virtual" so idk what's going on. Bug, perhaps, but I now am inclined to believe it is a hidden feature.

Important note though: such behavior is already possible. Struct dtors of members are called when the class dtor runs too...

>  However, if there are two mixin templates both declaring
> this(), the compiler dies with an ICE. :-D

eek.
March 15, 2017
On Wed, Mar 15, 2017 at 08:55:34PM +0000, Adam D. Ruppe via Digitalmars-d-learn wrote: [...]
> >  However, if there are two mixin templates both declaring
> > this(), the compiler dies with an ICE. :-D
> 
> eek.

Filed a bug for it:

	https://issues.dlang.org/show_bug.cgi?id=17259


T

-- 
Meat: euphemism for dead animal. -- Flora
March 16, 2017
On Wednesday, 15 March 2017 at 19:34:32 UTC, Ali Çehreli 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
>
> Wow! Is this specified anywhere or have you come across this by chance? :)
>
> Ali

It's not specified. Found it while trying to understand the difference between __dtor and __xdtor. But this is clearly designed to work this way. Not really an hidden gem.
March 16, 2017
On Thursday, 16 March 2017 at 07:49:19 UTC, Basile B. wrote:
> On Wednesday, 15 March 2017 at 19:34:32 UTC, Ali Çehreli 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
>>
>> Wow! Is this specified anywhere or have you come across this by chance? :)
>>
>> Ali
>
> It's not specified. Found it while trying to understand the difference between __dtor and __xdtor.

(which is __dtor doesn't call the other destructors in the aggregate, xdtor does)
1 2
Next ›   Last »