www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - little experiment with CTFE writef

Hello.

http://repo.or.cz/w/iv.d.git/blob/HEAD:/writer.d

it's not full-featured or so, just experiment in CTFE and
(semi)functional programming with templates.

excerpt from unittests:

  class A {
    override string toString () const { return "{A}"; }
  }

  char[] n =3D ['x', 'y', 'z'];
  char[3] t =3D "def";//['d', 'e', 'f'];
  wrwriter("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D\n");
  writef!"`%%`\n"();
  writef!"`%-3s`\n"(42);
  writef!"<`%3s`%%{str=3D%s}%*>\n"(cast(int)42, "[a]", new A(), n, t[]);


it generates mixins like this:

  -------
  wrwriter("`%`\x0a", -1);
  -------
  -------
  wrwriter("`", -1);
  wrWriteWidth!(' ',' ')(-1,-3,0,false,false,to!string(args[0]));
  wrwriter("`\x0a", -1);
  -------
  -------
  wrwriter("<`", -1);
  wrWriteWidth!(' ',' ')(-1,3,0,false,false,to!string(args[0]));
  wrwriter("`%{str=3D", -1);
  wrWriteWidth!(' ',' ')(-1,0,0,false,false,(args[1]));
  wrwriter("}", -1);
  wrWriteWidth!(' ',' ')(-1,0,0,false,false,to!string(args[2]));
  wrWriteWidth!(' ',' ')(-1,0,0,false,false,(args[3]));
  wrWriteWidth!(' ',' ')(-1,0,0,false,false,(args[4]));
  wrwriter(">\x0a", -1);
  -------


it's in no way ready for announcing in corresponding NG or even
for serious use, but someone can still be interested.

nothing that fancy there, except maybe the trick with keeping internal
state in structure which is passing from template to template. 2.065
(the one GDC using) aren't allowing such trick and i don't know if
2.066 does, but git head is certainly ok.


p.s. could we have separate group for showing such silly little
experiments to others, please? it's not ready for "announcing", it's not
for "learning", and it seems to badly fit in "general". maybe something
like "D.code"?
Oct 26 2014