Array of interface only with cast()?

Andre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 22 08:19:57 PDT 2014


Hi,

I just stumbled about this issue with array of interface. I want
to pass several objects (C and D) which shares the same interface A.
It seems somehow cumbersome that it only works if there is a cast on
the first element of the array.

interface A{}

class B: A{}

class C: B{};
class D: B{};

void main()
{
	//A[] arr = [new C(), new D()]; // Does not work
	A[] arr = [cast(A) new C(), new D()]; // Does work
}

Is the cast really needed?

Kind regards
André


More information about the Digitalmars-d-learn mailing list