This needs a different approach

Jesse Phillips jessekphillips at gmail.com
Mon Apr 28 12:38:58 PDT 2008


On Mon, 28 Apr 2008 19:22:15 +0200, Saaa wrote:

> "Robert Fraser" <fraserofthenight at gmail.com> wrote in message
> news:fv4t21$1ue1$1 at digitalmars.com...
>> 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?
> 
> Could you elaborate on that?
> The eat functions are totally different btw.

Say you have a Fruit interface with the function eat();
You could then have an Apple, Plum, Pear class that implements Fruit. If 
you have data as one of these types maybe in a variable of type Fruit you 
could then just call data.eat(). Though you don't seem to have it set up 
in this way, and my not be the needed approach for what you are trying to 
do. I'm tired so I'm not going in as much depth as I should.


More information about the Digitalmars-d-learn mailing list