little experiment with CTFE writef

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Sun Oct 26 19:46:14 PDT 2014


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 = ['x', 'y', 'z'];
  char[3] t = "def";//['d', 'e', 'f'];
  wrwriter("========================\n");
  writef!"`%%`\n"();
  writef!"`%-3s`\n"(42);
  writef!"<`%3s`%%{str=%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=", -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"?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141027/cc22a280/attachment-0001.sig>


More information about the Digitalmars-d mailing list