Why are interfaces so restrictive?

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Mar 18 07:42:00 PST 2006


"BCS" <BCS_member at pathlink.com> wrote in message 
news:dvfqn0$470$1 at digitaldaemon.com...
> No it wouldn't (if my understanding of interfaces is correct) all that it 
> would
> require is the generation of the function pointer table at compile time 
> and at
> run time, the joining of that pointer and a pointer to the struct.

It might be possible, but it would involve making a "second" kind of 
inheritance that wouldn't really fit into the current class and interface 
inheritance method.

> That might work, however, it requires allocating a class and a more 
> complicated
> call/access sequence. Either the class has to have access to the local 
> stack
> frame (by storing a pointer to it and adding another level of indirection) 
> or
> the data in the class needs to be available to the function (again by way 
> of a
> pointer). Nether of these is desirable and the same effect can be had 
> (with less
> cost) by using my idea.

It can.

void Foo()
{
 Item[char[]] bar;

 Database db = new class Database
 {
  Item SearchForItem(char[] n)
  {
   if(n in bar)
    return bar[n];
   throw new Error("blah!");

  }

  bool AddItem(char[] n, Item i)
  {
   if(n in bar)
    throw new Error("blah!");

   bar[n] = i;
   return true;
  }

  bool RemoveItem(char[] n)
  {
   if(n in bar)
   {
    bar.remove(n);
    return true;
   }

   throw new Error("blah!");
  }
 };

 DatabaseUI(db);
}

This looks strikingly like what you originally posted. 





More information about the Digitalmars-d mailing list