This needs a different approach

Saaa empty at needmail.com
Mon Apr 28 13:34:32 PDT 2008


> I was assuming your groupModule was a class, but now I think it might be a 
> module (duh!).

; )

>
> You should seriously consider putting these things into classes instead of 
> modules, and looking up the right interface based on the data type from a 
> function.  For example:
>
> interface Edible
> {
>   void eat();
> }
>
> class Pear : Edible
> {
>   void eat() {...}
> }
>
> static Edible[] fruits;
>
> static this()
> {
>    fruits = [APPLE:new Apple(), PEAR:new Pear()...].dup;
> }
>
> Edible get(uint type)
> {
>    if(type > fruits.length)
>       return null;
>    return fruits[type];
> }
>
> Robert is right, this is precisely the thing that OO programming was 
> created for :)
>
> -Steve

I should have written this before, but data is actually an array.

enum { APPLE, PEAR .. PLUM}

for(i=0;i<1000;i++)
{
switch (data[i].type)
{
   case APPLE:
     groupModule.appleModule.eat();
     break;
   case PEAR:
     groupModule.pearModule.eat();
     break;
 ..
 ..
  case PLUM:
    groupModule.plumModule.eat();
    break;
  default:
    break;
}
}





More information about the Digitalmars-d-learn mailing list