This needs a different approach

Robert Fraser fraserofthenight at gmail.com
Mon Apr 28 09:12:59 PDT 2008


Saaa wrote:
> I have this piece of code:
> 
> enum { APPLE, PEAR .. PLUM}
> 
> switch (data.type)
>   {
>    case APPLE:
>    groupModule.appleModule.eat();
>     break;
>    case PEAR:
>    groupModule.pearModule.eat();
>     break;
> ..
> ..
>    case PLUM:
>    groupModule.plumModule.eat();
>     break;
>    default:
>     break;
>   }
> 
> As the function is always the same I'd rather see a lookup iso an iteration 
> over all options.. but how?
> 
> groupModule.(data.type).eat();
> 
> Something like this is both faster and a lot less to code :) 

Isn't this what inheritance and polymorphism is all about?


More information about the Digitalmars-d-learn mailing list