www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20494] New: Appending derived class array to an interface

https://issues.dlang.org/show_bug.cgi?id=20494

          Issue ID: 20494
           Summary: Appending derived class array to an interface array.
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: minor
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: blackbirdcry806 gmail.com

import std.stdio;

interface IFoo
{
}

class Bar : IFoo
{
}

void main()
{
    IFoo[] f;
    Bar[] b = new Bar[5];
    foreach(ref e; b)
        e = new Bar();

    // this works well and as expected
    foreach(e; b)
        f ~= e;

    // this append the array with invalid values
    // f ~= b;

    writeln(f); 
}

Hello there,
So we have an interface IFoo, and a class Bar that implements IFoo.
The issue occurs when I try to append an array of Bar to an array of IFoo,
however when appending elements with a for loop works well and as expected.
After appendding the array it seems that the elements appended aren't valid
values.
I had this issue with both dmd 2.087, dmd 2.089.1 and according to godbolt
(https://godbolt.org/z/fTgzic) ldc 1.18.0

--
Jan 09