property magic for inerfaces (please!)

Lionello Lunesu lionello at lunesu.remove.com
Sat Nov 18 23:31:59 PST 2006


"Hasan Aljudy" <hasan.aljudy at gmail.com> wrote in message 
news:ejo4sm$h6$1 at digitaldaemon.com...
> Consider the following hypothetical interface:
>
> interface IPerson
> {
>     IPerson getFather();
>     IPerson getMother();
>     IPerson[] getSiblings();
>     IPerson[] getChildren();
> }
>
> For practical purposes, it would be nice to have some methods defined in 
> that interface, such as:
>
> bool hasChildren()
> {
>     return getChildren().length != 0;
> }
>
> but it's not possible to define this method right in the interface. 
> (Suppose say we really have to have the thing as an interface and not as 
> an abstract class).
>
> A possible solution is to add it to the interface without defining it,
>
> interface IPerson
> {
>     IPerson getFather();
>     IPerson getMother();
>     IPerson[] getSiblings();
>     IPerson[] getChildren();
>
>     bool hasChildren();
> }
>
> while hoping that all classes implemeting this interface would define the 
> method in the same way, i.e.
> bool hasChildren()
> {
>     return getChildren().length != 0;
> }
> The main problem with this is that it's not practical more complicated 
> functions.

What if getChildren() is a costly function, that, say, does an SELECT on a 
remote database? You would not want it to have that particular 
implementation since it would not need to collect the children first, just 
to check whether there are any.

Unfortunately, the only correct way to work with interfaces *is* declaring 
all the functions virtual. You want to do getChildren().length because you 
already know the implementation of getChildren, which is not a fair way to 
look at it.

L.





More information about the Digitalmars-d mailing list