"Named parameter builder" pattern for template parameters

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 21 06:45:52 PST 2014


On Fri, 21 Nov 2014 13:39:42 +0000
monarch_dodra via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> D has phenomenal meta programming possibilities, and I see more
> and more templates taking more and more parameters. So I thought
> to myself, why not have a template builder pattern?
i did something similar in my iv.writer (ctfe format string parser).
once i got about ten argumnets to each template (and growing), i moved
everything to structure.

  struct WrData {
    ... fields ...

    auto set(string name, T) (in T value)
    if (__traits(hasMember, this, name))
    {
      __traits(getMember, this, name) = value;
      return this;
    }
  }

and then i can pass the struct around, changing only the fields i want,
in "pseudo-functional" style:

  template MyTpl!(WrData data) {
    ...
    MyTpl!(data.set!"field0"(value0).set!"field1"(value1));
  }

that was a real saver. but now i have to wait for GDC update, 'cause
2.065 frontend is not able to use structs as template args.
-------------- 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/20141121/54579eb4/attachment.sig>


More information about the Digitalmars-d mailing list