Re: What are Protocols?

Serge Orlov <[email protected]>
Newsgroups gmane.comp.lang.prothon.user
Message-ID <[email protected]>
Paul Prescod wrote:
> You could ask the memory buffer object itself. If the people implemeting
> it were nice they will have provided an asFile method. So that's the 
> most basic possible adapter protocol. My first suggestion for Prothon, 
> then, is that iter_ should be spelled asIter_.

[snip]

> I'll say again that checking is not key. Probably the right way to do 
> protocol checking is at unit-testing time so that there is no runtime 
> overhead.*

Agreed.

> But if you really do implement a culture of STANDARD PROTOCOLS with many 
> implementations then you will start to become a victim of your own 
> success. The method name "asRowset_" will be grabbed by some database 
> guy working at company A and also by someone working at company B. The 
> companies merge and their codebases use asRowset_ to mean two different 
> things. It's a classic namespace management problem. There is no way to 
> detect in a decentralized manner when you "really should" be working 
> with someone else.

asSomething_ namespace will be reserved for standard library. Or course
if you send money to Mark he will allocate names for all interfaces in
your library :-)

> Okay, so at the risk of going off half-cocked, let's talk about some syntax.

> Let's say you have an object and you want to get an iterator interface 
> for it (i.e. client code). You would do this:

> object.adapt_(StdInterfaces.Iterator)

That doesn't correlate with asIter_. Maybe StdInterfaces.Iter ?

It is also not clear why interfaces should live in StdInterfaces?
All exceptions and types are stored as Object attributes, why
interfaces can't be?

Here is my idea how the standard iterator interface can be implemented
with a registry.

object Iter:
    """All objects conforming to standard iterator protocol must
    implement .next() method that should return the next item from
    the container each time it is called. If there are no any more
    items ..."""

    adapters = {}
    def call_(obj):
        """Return object conforming to interface Iter"""
        if obj.attrs_.hasKey?($asIter):
            return obj.asIter()
        adapter = adapters.get(type(obj), None)
        if adapter:
            return adapter(obj)
        raise NotIter()
    def registerAdapter(from_type, adapter):
        adapters[from_type] = adapter

Note that I'm not sure we always need registry. It seems to me the
registry is needed in two cases:

1. Interface was implemented in the standard library after objects
conforming to it appeared in the wild.
2. Developers didn't have time to implement asIter_() or forgot about
it.

#1 is not applicable to Iter interface, #2 seems like a poor excuse.

  -- Serge
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.