Feature request - simpler constructors

Daniel Keep daniel.keep.lists at gmail.com
Fri Sep 21 05:54:13 PDT 2007


This is just silly.  There's no reason to make this a *language*
feature.  I mean, we have templates and mixins for a reason!  To whit:

module lazy_janice;

import std.metastrings;
import std.stdio : writefln;
import std.string : format;

template Tuple(T...)
{
    alias T Tuple;
}

template lazyJaniceInit(uint i)
{
    const lazyJaniceInit = "";
}

template lazyJaniceInit(uint i, alias Member, Members...)
{
    const lazyJaniceInit =
        Member.stringof~" = args["~ToString!(i)~"];\n"
        ~ lazyJaniceInit!(i+1,Members);
}

template lazyJaniceArgs()
{
    alias Tuple!() lazyJaniceArgs;
}

template lazyJaniceArgs(alias Member, Members...)
{
    alias Tuple!(typeof(Member), lazyJaniceArgs!(Members))
            lazyJaniceArgs;
}

template lazyJanice(Members...)
{
    this(lazyJaniceArgs!(Members) args)
    {
        mixin(lazyJaniceInit!(0, Members));
    }
}

class Foo
{
    int a;
    float b;

    mixin lazyJanice!(a,b);

    string toString()
    {
        return "Foo(%s,%s)".format(a,b);
    }
}

void main()
{
    auto foo = new Foo(42,3.1413);
    writefln(foo);
}

Easy as can be.  You might want to rename the template, though... :3

	-- Daniel



More information about the Digitalmars-d mailing list