InstanceOf

Chris Cain clcain at uncg.edu
Sun Jun 23 04:13:30 PDT 2013


On Sunday, 23 June 2013 at 11:04:59 UTC, Lemonfiend wrote:
> ...
> 	void writeAll()
> 	{
> 		foreach(elem; array)
> 			elem.write();
> 	}
>
> 	void writeB()
> 	{
> 		// Only call .write on B's
> 		// How do I get the B's from the array of I's?
> 	}

http://dlang.org/expression.html#CastExpression

So, this should do it:

void writeB()
{
  	foreach(elem; array)
		if(cast(B) elem)
  			elem.write();
}

You can also do some things like this, if you want to use 
something specific to Bs:

if(auto b = cast(B) elem)
	b.bSpecificMethod();


More information about the Digitalmars-d-learn mailing list