Why are interfaces so restrictive?

BCS BCS_member at pathlink.com
Fri Mar 17 18:19:13 PST 2006


In article <dvfoqe$29i$1 at digitaldaemon.com>, Jarrett Billingsley says...
>
>"BCS" <BCS_member at pathlink.com> wrote in message 
>news:dvfm45$1j5f$3 at digitaldaemon.com...
>> why not let a struct implement an interface
>
>Because that would complicate structs, something that I think Walter is not 
>willing to do.
>

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.

>> or even let a function define an interface literal..
>
>You could define an anonymous class literal which implemented that 
>interface.
>

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.

>class Item
>{
>
>}
>
>interface Database
>{
> Item SearchForItem(char[]);
> bool AddItem(char[], Item);
> bool RemoveItem(char[]);
>}
>
>void DatabaseUI(Database deebs)
>{
>
>}
>
>
>void Foo()
>{
> DatabaseUI(new class Database
> {
>  Item[char[]] bar;
>
>  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!");
>  }
> });
>}
>
>void main()
>{
>
>} 
>
>





More information about the Digitalmars-d mailing list