Thread overview
Static Foreach
Sep 19, 2006
nazo
Sep 19, 2006
BCS
Sep 20, 2006
nazo
September 19, 2006
I have some suggestions.
How about support to "static foreach" as "static if"?
Problem is that it is not still able to use array in template arguments.

Example1:
import std.stdio;

int test(char[][] strs)(char[] a){
  switch(a){
    static foreach(const uint index,const char[] str;strs){
       case str:
         return index;
    }
  }
  return -1;
}

int main(char[][] args){
  if(args.length<2)return -1;
  writefln(test!([cast(char[])"test","test2","test3"])(args[0]));
  return 0;
}

Example2:
//if dmd supported "identifier"(See Template-based Processing Thread)
import std.stdio;

template T(char[][] a){
  interface Base(){
    void print();
    Base next();
  }
  static foreach(const int index,const char[] b;a){
    class identifier(b):Base{
      void print(){
        writefln(b);
      }
      Base next(){
        return new identifier(a[index+1%a.length]);
      }
    }
  }
}

mixin T!([cast(char[])"Test","Test2","Test3"]);

int main(){
  Base a=new Test;
  for(int i;i<10;i++){
    a.print;
    a=a.next();
  }
  return 0;
}

**Following is an addition**

-compile Attribute
there is no variable for compile time only.(const is merely constant)

Example:
old:
template T(int a){
  const int i=a;
  const int i2=i*i;
  const int i3=i2*i2;
  const int i4=i3*i3;
  const int i5=i4*i4;
  const int T=i5;
}
auto i=T!(10).T;

new:
template T(int a){
  compile int i=a;
  i*=i;
  i*=i;
  i*=i;
  i*=i;
  const int T=i;
}
auto i=T!(10);

-static for
template T(int a){
  compile int i=a;
  static for(compile int i2=0;i2<4;i2++){
    i*=i;
  }
  const int T=i;
}
auto i=T!(10);

-- 
Nazo

I want that D should be more chaosful!
September 19, 2006
nazo wrote:
> I have some suggestions.
> How about support to "static foreach" as "static if"?
> Problem is that it is not still able to use array in template arguments.


why limit it to arrays, or for that matter, why like types?

(I have suggested this before http://www.digitalmars.com/d/archives/digitalmars/D/32232.html)


struct Fig
{
	char c;
	int i;
	real r;

	void Display()
	{
		witheach(memb; this)
			writef(memb, " ");
	}


	bool opCmp(Fig other)
	{
		witheach(alias o, memb; this)
			if(other.o != memb) return false;
		return true;
	}
}
September 20, 2006
I want to write like this code for optimize. The compiler cannot optimize code like this code. CGI needs more speeds!

char[] replace(char[][] before,char[][] after)(char[] str){
  static assert(before.length==after.length);
  compile char[][] t=before;
  compile char[][] u=after;
  /*sort*/
  static for(compile int i;i<t.length-1;i++)
    static for(compile int j=j+1;j<t.length;j++)
      static if(t[i]>t[j]){
        compile char[] tmp=t[i];
        t[i]=t[j];
        t[j]=tmp;
        compile char[] tmp=u[i];
        t[i]=u[j];
        u[j]=tmp;
      }
  /**/
  int len=str.length;
  compile before_len=0;
  for(int i;i<str.length-b[0].length;i++){
    static foreach(compile int j,compile char[] b;t){
      static if(before_len!=b.length){
        if(b.length>len)
          return str;
      }
      if(str[i..i+b.length]==b){
        str=str[0..i]~u[j]~str[i+b.length..$];
        i+=u[j].length-1;
        len-=u[j].length;
        break;/*for*/
      }
      before_len=b.length;
    }
    len--;
  }
  return str;
}

private import std.stdio;
void main(char[][] args){
  if(args.length<2)return -1;
  writefln(replace!(["aa","bb"],["cc","dd"])(args[1]));
}