I’ve investigated this to double check and arrays of interfaces are already supported in Objo. For example:
Interface IAnimal
Sub Speak()
End Interface
Class Cat Implements IAnimal
Sub Speak()
Print("Meow")
End Sub
End Class
Class Dog Implements IAnimal
Sub Speak()
Print("Woof")
End Sub
End Class
Var animals As Array(Of IAnimal)
animals.Append(New Cat())
animals.Append(New Dog())
For Each animal As IAnimal In animals
animal.Speak()
Next
This compiles and runs correctly, so you do not need to introduce an abstract superclass. Declaring the array with the interface element type is the intended approach.
However, I found a narrower compiler bug while investigating. This is currently rejected:
Var cats As Array(Of Cat) = [New Cat()]
Var animals As Array(Of IAnimal) = cats
A Cat can be assigned to an IAnimal, and Objo already permits covariant assignment for built-in arrays, so the corresponding array conversion should also work. The compiler currently loses the interface relationship while comparing the array element types.
The same bug affects passing, returning, or appending an already-typed Array(Of Cat) where an Array(Of IAnimal) is expected.
For now, declare the collection as Array(Of IAnimal) from the outset and append the concrete objects to it. Could you post your complete failing code and the compiler error? That will confirm whether this is the case you encountered. I can then definitively patch this narrow bug