On 08/03/2016 10:58 AM, Andre Pany wrote:

> I try to initialize an array of objects. The methods (linear/equals/....)
> returns object of different classes, but all implement a common
> interface "Element".
>
> Element[] elements =
> quadraticCoefficient(1)~linearCoefficient(2)~equals()~constant(1);

Note that those array concatenation operators are being applied to individual object. Unless you define that operator for your types, it won't work.

> I tried different casts and different ways but it seems, currently D
> does not support this syntax?

Not that syntax but D is supposed to figure out the closest common ancestor. Unfortunately, the following code requires that cast:

interface Element {
}

class Foo : Element {
}

class Bar : Element {
}

Foo quadraticCoefficient(int) {
    return new Foo();
}

Bar linearCoefficient(int) {
    return new Bar();
}

Foo equals() {
    return new Foo();
}

Bar constant(int) {
    return new Bar();
}

void main() {
Element[] elements = cast(Element[])[ quadraticCoefficient(1), linearCoefficient(2), equals(), constant(1) ];
    elements ~= quadraticCoefficient(1);
    elements = elements ~ linearCoefficient(2);
    // etc.
}

Ali

Reply via email to