Array of concrete class not covariant with array of interface
d
Solution
You have run into the problem of array covariance.
Let's assume that your code compiled fine. Now, consider the following code:
class SomeOtherColumnInfo : IDbColumnInfo {}
IDbReader reader = new MySqlReader(...);
IDbColumnInfo[] columns = reader.columns;
columns[3] = new SomeOtherColumnInfo(); // OK
Since the array is mutable, we can overwrite its elements with instances of other `IDbColumnInfo`-derived classes. The problem with that is that we're also modifying the private `_columns` field of `MySqlReader`. So, now we have a `SomeOtherColumnInfo` instance as a member of the `MySqlColumnInfo[]` array. Thus, we've broken the type system without using casts or other unsafe code. Since the compiler is expected to stop us from doing that by accident, it will refuse to implicitly cast mutable arrays of classes to arrays of other classes, even if those classes are related.
Now, I think it would make sense for D to allow compilation if the returned array was not mutable (that is, const or immutable). However, the compiler doesn't like that either. I don't know if that's an omission, or if there's a reason unknown to me.
Problem
I'm having a little problem in providing an abstract base layer for my `dataaccess.mysqlclient` module where I have defined a bunch of interfaces for minimum requirements and a bunch of classes that implement them. Now the dmd compiler complains: `Error: function dataaccess.mysqlclient.MySqlReader.columns of type @property MySqlColumnInfo[]() overrides but is not covariant with dataaccess.dbclient.IDbReader.columns of type @property IDbColumnInfo[]() Exit code 1` the relevant lines of code look like this: IDbReader: ``` interface IDbReader { @property IDbColumnInfo[] columns(); // ... } ``` MySqlReader: ``` class MySqlReader : IDbReader { private MySqlColumnInfo[] _columns; @property public MySqlColumnInfo[] columns() {return _columns;} // ... } ``` There are a few ways I could probably work around this compiler issue; - Declare the concrete property to be of `IDbColumnInfo[]` - wrap the array in a list class And probably a couple more if i think about it a bit longer. None of those seem quite elegant though. Here come the big questions: - Am I overlooking something simple? - Can arrays of implementations ever be covariant with arrays of interfaces? Also I can't really imagine the reason for the compilers' complaint. There are more complex structures in my code that have been compiling just fine. So if somebody can explain why this won't work as is, That'd be much appreciated.