August 06, 2010
dcoder wrote:

> Suppose I have a base class with many ctors().
> 
> I want to inherit from the base class and make one slight alteration to it, but I don't want to write X times the following:
> 
> this(args) {
>   super(args);
> }
> 
> Does D have an easy way for the derived class to 'inherit' all or some of
> the base class ctors(), and just override/add ctors() in the derived
> class?
> 
> In Java, we can use eclipse to auto-generate code for us.  :)  But, the results look cluttered, and it's really not a solution.
> 
> Anyways, just curious.
> 
> thanks.

Here is a possible solution to your problem:

-Rory

August 06, 2010
On Fri, Aug 6, 2010 at 21:59, Rory Mcguire <rjmcguire@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)???


August 09, 2010
Philippe Sigaud wrote:

> On Fri, Aug 6, 2010 at 21:59, Rory Mcguire <rjmcguire@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)???

I'll see what I can do to shorten it but I'm not sure how to iterate over the Selectors inside the foreach and still be able to skip unselected constructors. Hmmm I suppose I could use a temporary boolean or something.


I don't use "A delegate(someTuypes)" because the compiler says that the type
of the constructors is for instance: "A function(int x)"


1 2
Next ›   Last »