On Tue, 18 Jun 2013 18:10:49 -0400, Stephen Jones <siwe...@gmail.com> wrote:

I know I can cast, but how do I know what base class each b in the foreach loop is?

Just an FYI, you are using the wrong terminology. In this case, Bar is the base class, and Foo and Foos are the *derived* classes.

Other than that, I think Ali gave you the best solution.

If you wanted to do something that *wasn't* common between two derived classes (i.e. some function/member that was only on one specific derived class), you can use a nice technique called auto-casting:

if(auto der = cast(Foo)b)
{
   // use Foo specific functionality on der
}
else if (auto der = cast(Foos)b)
{
   // use Foos specific functionality on der
}

Hm... would be a nice idiom to implement generically in D. Like a type switch.

-Steve

Reply via email to