inheriting ctors?
Philippe Sigaud
philippe.sigaud at gmail.com
Fri Aug 6 13:54:46 PDT 2010
On Fri, Aug 6, 2010 at 21:59, Rory Mcguire <rjmcguire at gm_no_ail.com> wrote:
>
> Here is a possible solution to your problem:
>
> -Rory
I believe you can get the type of A. Isn't it typeof(super) or
std.traits.BaseClassesTuple!B[0] ? B in the latter case being typeof(this)
That way, there is no need for the user to provide A, it's automatically
found by the template.
Warning: I did not test this.
And, we know the constructs are of type 'A function(someTypes)' [*], so the
'A function' part is redundant.
Hence, the user only needs to provide for the args types and that makes for
a cleaner call.
* either as a list :
mixin(InheritConstructors!(int, double, string)); // I want to inherit
the constructors taking one type, build me the __ctors for int, double and
string
* or, in the case of multi-parameters constructors, wrap them in a tuple:
mixin(InheritConstructors!(int, double, Tuple!(int, double)); // I want
super(int), super(double) and super(int, double)
That means iterating on the type list, and determining if the current type
is a tuple or not
* if its a 'normal' type, create the corresponding contructor
* if it's a Tuple, crack it open and get the types, using the .Types alias
std.typecons.Tuples have. Creating a constructor from this typetuple is no
different from creating it for one type.
To determine if something is a std.typecons.Tuple, you cannot use an is()
expression: they do not allow multiple types:
enum bool isTuple = is(T == Tuple!U, U...); // no. U... is not allowed. Hmm,
enhancement request?
So, you can either rely on it having a .Types 'member':
template isTuple(T)
{
enum bool isTuple = is(T.Types);
}
Pb: that will flag as tuples any type that exposes a ".Types" alias.
Or use a function accepting a Tuple:
template isTuple(T)
{
enum bool isTuple = is(typeof({
void foo(U...)(Tuple!U t) {}; // this
function accepts only tuples
foo(T.init); // test it
}()));
}
It's ugly as a rat's ass, but it's more solid than the former template.
Philippe
[*] btw, shouldn't that be A delegate(someTypes)???
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100806/e580e8f4/attachment-0001.html>
More information about the Digitalmars-d-learn
mailing list