I iterated on Ali's solution with more OOP to demonstrate
features you may find interesting.
import std.stdio;
interface IBar
{
@property int val();
}
class Bar : IBar
{
protected int _val;
@property int val()
{
return this._val;
}
}
class Foo : Bar
{
this()
{
this._val = 10;
}
}
class Foos : Bar
{
string str = "some more memory";
this()
{
this._val = 20;
}
}
void main()
{
IBar[] bars; // <--- collection of objects conforming to your
interface.
bars ~= new Foo();
bars ~= new Foos();
foreach(IBar b; bars)
{
writeln(b.val);
}
}