On Friday, 27 May 2016 at 18:10:59 UTC, Vladimir Panteleev wrote:
This is by far the most appealing way to implement named arguments that I've seen so far:

https://github.com/CyberShadow/ae/blob/master/utils/meta/args.d

Another cool thing this enables: object initializers.

  T init(T, Args...)() {
    import std.traits : FunctionTypeOf;

    static struct DummyType {}
    static if (is(T == class)) T r = new T;
    else T r;

    foreach (arg; Args) {
      alias fun = arg!DummyType;
      static if (is(FunctionTypeOf!fun PT == __parameters)) {
        enum name = __traits(identifier, PT);
        foreach (m; __traits(allMembers, T)) {
static if (name == m) __traits(getMember, r, m) = fun(DummyType.init);
        }
      }
    }
    return r;
  }

  struct Point {
    int x, y;
  }

  void main() {
    auto pt = init!(Point, x => 123, y => 456);
    assert(pt.x == 123 && pt.y == 456);
  }

Reply via email to