On 05/25/2017 12:52 PM, Moritz Maxeiner wrote:
Be aware, though, that constructors mixed in via a mixin template behave differently with regards to overloading[1].

[1] https://issues.dlang.org/show_bug.cgi?id=11500

Of course it couldn't be that simple :(

Adam's workaround (`alias __ctor = mixin_thing.__ctor;`) might be workable, though.

If that makes the usage too verbose, then a string mixin is the way to go, I guess. It's immune to issue 11500, but AutoConstructor isn't as nice to look at:

----
static string AutoConstructor(fields ...)()
{
    import std.meta: staticMap;
    import std.traits: fullyQualifiedName;
    import std.string: join;

    enum fqns = staticMap!(fullyQualifiedName, fields);
    auto fields_str = "std.meta.AliasSeq!(" ~ [fqns].join(", ") ~ ")";

    return "
        static import std.meta;
        this(typeof(" ~ fields_str ~ ") args)
        {
            " ~ fields_str ~ " = args;
        }
    ";
}

class Person
{
    string name;
    int age;
    mixin(AutoConstructor!(age, name));
    this(float f) {}
}

void main()
{
    auto p = new Person(42, "Arthur");
    assert(p.age == 42);
    assert(p.name == "Arthur");
}
----

Weird: AutoConstructor needs to be static. Doesn't make sense to me. Looks like a compiler bug.

Reply via email to