Friday, 9 August 2013

Array of concrete class not covariant with array of interface

Array of concrete class not covariant with array of interface

I'm having a little problem in providing an abstract base layer for my
dataaccess.mysqlclient module where I have defined a bunnch 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.

No comments:

Post a Comment