Mixin on a bunch of foreach fails.

Charles McAnany (dlang) dlang at charlesmcanany.com
Thu Jan 12 20:07:23 PST 2012


Hi, all. So I'm trying to make some very ugly code generic. The main 
ugliness isn't in the code shape, it's in the running time. It's O(n^m) 
Eww! (don't worry, n is only about 6.)

Anyhoo, Here's what I want:

    void foo(int size)(int[] arr){
         mixin(forStart!(size));
             doStuff(pos0, pos1, pos2,...); // this line is generated by
    another mixin that works correctly.
    }

    //Generates for loop headers.
    private static string forStart( int sz)(){
         string forStrings = "";
         for(int i = 0; i < sz; i++){
             forStrings ~="foreach(pos"~text(i)~"; 0..arr.length)\n ";
         }
         return forStrings;
    }

It is my great displeasure to report:
src\hw06.d(35): found 'EOF' instead of statement
src\hw06.d(18): Error: template instance hw06.tryCombinations!(5) error 
instantiating

But here's the wacky part:
I can execute this function and print it to stdout. If I do, I get...
foreach(pos0; 0..arr.length)
foreach(pos1; 0..arr.length)
foreach(pos2; 0..arr.length)
foreach(pos3; 0..arr.length)
foreach(pos4; 0..arr.length)

and I can copy-paste this exact code into where I currently have the 
mixin, and the code behaves correctly.

Is there some subtle aspect of mixin that I'm missing here?

Cheers,
Charles.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120112/38c86108/attachment.html>


More information about the Digitalmars-d-learn mailing list