std.mixins

Lutger lutger.blijdestijn at gmail.com
Tue Aug 31 11:40:09 PDT 2010


dsimcha wrote:

...
>> My suggestion is to include a mixin for unrolling loops.
> 
> I thought about this.  Simple loop unrolling doesn't seem like a very useful
> optimization on modern hardware because branch prediction and pipelining have
> gotten so good.  It can still be useful if you also change the loop body a
> little, for example using multiple accumulators to increase instruction level
> parallelism,
> but this is hard to write generically.  I can't think of a way to write such a
> mixin such that it would be both generic and useful.

Probably not for performance, but as a utility for metaprogramming I find it 
sometimes convenient. The loop index would be written as a literal and thus can 
be used as a compile time constant. I think in phobos recursive templates are 
the way to do this? 

I have hacked something like this, the '@' will be replaced with the loop index:

enum someConstarray = makeConstArray();

mixin(unroll( 0, someConstarray.length, q{ 
    processMember!(someConstArray[@])() 
})); 

 
>> Also something like
>> defineEnum, but for generating bitflags. (I have some code for this).
> 
> Can you give an example?  I'm not sure what you mean.

I have this example from an earlier discussion, I think with bearophile:

mixin( defineBitflags("Todo", q{ 
    do_nothing, 
    walk_dog, 
    cook_breakfast, 
    morning_task = walk_dog | cook_breakfast
}) );

turns into:

enum Todo { 
    do_nothing = 1,
    walk_dog = 2,
    cook_breakfast = 4,
    morning_task = walk_dog | cook_breakfast
}

With some bit twiddling you can actually create a range for it too, to iterate 
all the flags set.
 
>> I would
>> also like to see a mixin for generating the boilerplate for decorator
>> forwarding, which is annoying. This should be not that hard with the existing
>> code in std.typecons.
> 
> Shouldn't opDispatch basically handle this for you?  I think that's one reason
> why it was created.

Not exactly, i don't think you can implement an interface with opDispatch. But 
perhaps this is too fringe for phobos which isn't exactly big on OOP anyway. 
 
>> The dranges project at dsource is awesome, it wouldn't hurt to put
>> some of that good stuff in phobos.
> 
> Agreed.  It's Philippe Sigaud's project and he's been recently added to the
> roster
> of Phobos devs.  I assume he intends to integrate at least the more generally
> useful parts of dranges eventually.

I hope so.


More information about the Digitalmars-d mailing list