array of elements of various subclasses

spir denis.spir at gmail.com
Sun Nov 7 11:42:21 PST 2010


On Sun, 07 Nov 2010 14:08:17 -0500
Jesse Phillips <jessekphillips+D at gmail.com> wrote:

> spir Wrote:
> 
> > No, in fact a variadic param list is not an array -- this is according to TDPL an iternal type of the language. Trying to assign it to an X[] var throws:
> > DeeMatch.d(371): Error: cannot implicitly convert expression (patterns) of type DeeMatch.Pattern to Pattern[]
> > Well, actually, the type of the expression given in the error (DeeMatch.Pattern) is wrong: this is the type of each element of the variadic list.
> 
> Well I took the example for type safe variadics and added an int[] fish storing the array... So am I missing something?
> 
> http://ideone.com/Kyi78

No, you are right... I have certainly done an error when trying. Thank you.
Note: this works also with elements of various subtypes (so I can use this method to replace array parameters of type constructors):

class E {}
class E1 : E {
    int i;
    this(int i...) { this.i = i; }
    override string toString () { return format("E1(%s)", this.i); }
}
class E2 : E {
    float f;
    this(float f...) { this.f = f; }
    override string toString () { return format("E2(%s)", this.f); }
}

class C {
    E[] ee;
    this(E[] ee...) {
        this.ee = ee;
    }
}
//~ class C {
//~     int[] ii;
//~     this(int[] ii...) {
//~         this.ii = ii;
//~     }
//~ }

void main () {
//~     auto c = new C(1,2,3);
//~     writeln(c.ii);
    auto c = new C(new E1(1), new E2(1.1));
    writeln(c.ee);
}


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d-learn mailing list