Thread overview
Re: Getting the parameters of a struct/class constructor
Jan 24, 2013
Andrej Mitrovic
Jan 24, 2013
Andrej Mitrovic
January 24, 2013
On 1/24/13, Joseph Rushton Wakeling <joseph.wakeling@webdrake.net> wrote:
>      ParameterTypeTuple!(A.this)

Use ParameterTypeTuple!(A.__ctor)
January 24, 2013
On 1/24/13, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 1/24/13, Joseph Rushton Wakeling <joseph.wakeling@webdrake.net> wrote:
>>      ParameterTypeTuple!(A.this)
>
> Use ParameterTypeTuple!(A.__ctor)
>

If you have multiple constructors you can pick the parameters with a helper template:

import std.traits, std.string;

struct A
{
	this(int a, double b)
	{
	}

    this(float y)
    {
    }
}

template PickCtorParams(Type, size_t index)
{
    enum ctorLen = __traits(getOverloads, Type, "__ctor").length;
    static if (index < ctorLen)
    {
        alias ParameterTypeTuple!(__traits(getOverloads, A,
"__ctor")[index]) PickCtorParams;
    }
    else
    {
        static assert(0,
            format("index %s exceeds %s ctors for type %s", index,
ctorLen, Type.stringof));
    }
}

void main()
{
    pragma(msg, PickCtorParams!(A, 0));  // (int, double)
    pragma(msg, PickCtorParams!(A, 1));  // (float)
    pragma(msg, PickCtorParams!(A, 2));  // out of bounds
}
January 24, 2013
On 01/24/2013 06:14 PM, Andrej Mitrovic wrote:
> On 1/24/13, Joseph Rushton Wakeling <joseph.wakeling@webdrake.net> wrote:
>>       ParameterTypeTuple!(A.this)
>
> Use ParameterTypeTuple!(A.__ctor)

Brilliant.  Thanks very much. :-)