digitalmars.D.learn - Array of interface only with cast()?
- Andre (17/17) Apr 22 2014 Hi,
- Andrej Mitrovic (3/4) Apr 22 2014 It's a known issue and a filed bug report. I don't have the Issue
- Andre (5/9) Apr 22 2014 Nice to hear that it is not by design but will be fixed.
- bearophile (5/6) Apr 22 2014 See:
- Steven Schveighoffer (6/21) Apr 22 2014 At this point, yes. This is because the expression [new C(), new D()] do...
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é
Apr 22 2014
On Tuesday, 22 April 2014 at 15:19:55 UTC, Andre wrote:Is the cast really needed?It's a known issue and a filed bug report. I don't have the Issue number at hand though, someone else will likely provide it.
Apr 22 2014
Am 22.04.2014 17:23, schrieb Andrej Mitrovic:On Tuesday, 22 April 2014 at 15:19:55 UTC, Andre wrote:Nice to hear that it is not by design but will be fixed. Thanks a lot. Kind regards AndréIs the cast really needed?It's a known issue and a filed bug report. I don't have the Issue number at hand though, someone else will likely provide it.
Apr 22 2014
Andre:Nice to hear that it is not by design but will be fixed.See: https://issues.dlang.org/show_bug.cgi?id=3543 Bye, bearophile
Apr 22 2014
On Tue, 22 Apr 2014 11:19:57 -0400, Andre <andre s-e-a-p.de> wrote: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?At this point, yes. This is because the expression [new C(), new D()] does not take into account what you are assigning to (IMO it should). The compiler will use the most derived type possible that all the elements implicitly cast to. In this case, it will be a B[]. -Steve
Apr 22 2014