Re: Protocol compiler hint vs no hint....
Re: Protocol compiler hint vs no hint....
- Subject: Re: Protocol compiler hint vs no hint....
- From: Will Mason <email@hidden>
- Date: Sun, 10 Jul 2005 13:34:14 -0700 (PDT)
> Question: Is there any benefit of using a protocol vs just declaring
>
> the static type?
The benefit to using a protocol happens when you don't know what type
exactly might implement the methods that you need, but you know you
need certain methods from the protocol. If you already know the exact
type, then just use the static type, otherwise use the protocol.
For example, I've got a framework that provides a bunch of iterators
and containers. One type of iterator adds items to the back of a
container rather than assigning the item to a reference. My protocol
OLBackInserter is used to help make sure that the container has the
right method for the back insert iterator to work. I don't care what
the container is as long as it adopts the OLBackInserter protocol.
I've seen a wide range of attitudes toward formal protocols on this
list, which seem to go from utter disdain to toleration. The general
complaint, and I'll let experts on this take fill in the gaps, is that
there is no way to guarantee that an object reference that you are
passed will actually implement the protocol, even though the method
signature states that it will.
This is true; there is no guarantee, and proponents of avoiding formal
protocols can suggest many valuable alternatives. However, I like
formal protocols because I view the complaints of the compiler as being
the first line of defense against doing something really wrong, even
though at run time I still might get into trouble.
I realize I've probably just invited loads of trouble. Oh, well...
Just my thoughts,
Will
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden