On Friday, 21 November 2014 at 14:46:00 UTC, ketmar via
Digitalmars-d wrote:
On Fri, 21 Nov 2014 13:39:42 +0000
monarch_dodra via Digitalmars-d <digitalmars-d@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.

Hum, use a standard structure combined with CTFE to achieve the
same goal. Smart. BTW, if you use opDispatch, you could probably
have it as:
MyTpl!(data.set_field0(value0).set_field1(value1));

Which may or may not read nicer, depending on how you look at it.

With proper template overloading, you should even be able to
implement this as a backwards compatible template.

Reply via email to