GenerateThis

GenerateThis is a mixin string that automatically generates a this() function, customizable with UDA.

enum string GenerateThis;

Examples

class Class
{
    int field;

    mixin(GenerateThis);
}

auto obj = new Class(5);

obj.field.shouldEqual(5);
class Class
{
    int field;

    mixin(GenerateThis);
}

class Child : Class
{
    int field2;

    mixin(GenerateThis);
}

auto obj = new Child(5, 8);

obj.field.shouldEqual(5);
obj.field2.shouldEqual(8);
class Class
{
    int field;

    void method() { }

    mixin(GenerateThis);
}

auto obj = new Class(5);

obj.field.shouldEqual(5);
class Class
{
    int[] array;

    mixin(GenerateThis);
}

auto array = [2, 3, 4];
auto obj = new Class(array);

array[0] = 1;
obj.array[0].shouldEqual(2);
import std.typecons : Nullable, nullable;

class Class
{
    Nullable!(int[]) array;

    mixin(GenerateThis);
}

auto array = [2, 3, 4];
auto obj = new Class(array.nullable);

array[0] = 1;
obj.array.get[0].shouldEqual(2);

obj = new Class(Nullable!(int[]).init);
obj.array.isNull.shouldBeTrue;
class Class
{
    @Default!5
    int value = 5;

    mixin(GenerateThis);
}

auto obj1 = new Class();

obj1.value.shouldEqual(5);

auto obj2 = new Class(6);

obj2.value.shouldEqual(6);
struct Struct
{
    mixin(GenerateThis);
}

auto strct = Struct();
import std.conv : to;

class Class
{
    @Default!(() => new Object)
    Object obj;

    mixin(GenerateThis);
}

auto obj1 = new Class();
auto obj2 = new Class();

(cast(void*) obj1.obj).shouldNotEqual(cast(void*) obj2.obj);
class Parent
{
    int field1;

    @Default!2
    int field2 = 2;

    mixin(GenerateThis);
}

class Child : Parent
{
    int field3;

    @Default!4
    int field4 = 4;

    mixin(GenerateThis);
}

auto obj = new Child(1, 2, 3, 4);

obj.field1.shouldEqual(1);
obj.field3.shouldEqual(2);
obj.field4.shouldEqual(3);
obj.field2.shouldEqual(4);
class Class
{
    static int field1;
    int field2;

    mixin(GenerateThis);
}

auto obj = new Class(5);

obj.field1.shouldEqual(0);
obj.field2.shouldEqual(5);
class Class
{
    immutable(Object)[] array;

    mixin(GenerateThis);
}
@(This.Private)
class PrivateClass
{
    mixin(GenerateThis);
}

@(This.Protected)
class ProtectedClass
{
    mixin(GenerateThis);
}

@(This.Package)
class PackageClass
{
    mixin(GenerateThis);
}

class PublicClass
{
    mixin(GenerateThis);
}

static assert(__traits(getProtection, PrivateClass.__ctor) == "private");
static assert(__traits(getProtection, ProtectedClass.__ctor) == "protected");
static assert(__traits(getProtection, PackageClass.__ctor) == "package");
static assert(__traits(getProtection, PublicClass.__ctor) == "public");
class Class
{
    @Default!(() => new Object)
    Object foo;

    mixin(GenerateThis);
}

((new Class).foo !is null).shouldBeTrue;
class Class
{
    @(This.Exclude)
    int i = 5;

    mixin(GenerateThis);
}

(new Class).i.shouldEqual(5);
struct Struct
{
}

class Class
{
    Struct[] values_;

    mixin(GenerateThis);
}

const Struct[] constValues;
auto obj = new Class(constValues);
class Class
{
    int a;

    @property int foo() const
    {
        return 0;
    }

    mixin(GenerateThis);
}

static assert(__traits(compiles, new Class(0)));
static assert(!__traits(compiles, new Class(0, 0)));

Meta