proposal for call_ and str_

"Mark Hahn" <[email protected]>
Newsgroups gmane.comp.lang.prothon.user
Message-ID <[email protected]>
I'd like to make a proposal for handling the issue of having different
call_() and str_() methods for a prototype and extenstions (instances) of a
prototype.  This proposal is independent of prototypes and classes.  I have
made a different proposal for classes that does not include this proposal.

This proposal is actually for any method having two versions of attributes,
one for the prototype and one for it's extensions.  These can be any method
or data attributes.

The proposal is very simple.  It is the same "special lookup rule" I had in
the "class proposal 2" message.  I am not expecting it to solve all the
class problems this time though, just this particular attribute duality
problem.

            ---- attribute proxy proposal ----

If an object has an attribute called "attrProxy_" (from now on called the
proxy object), then all direct attribute lookups for that object will happen
on the proxy object instead of the actual object.

By "direct lookup" it is meant any lookup not through inheritance.  So
obj.attr is a "direct lookup" of attr on obj.  Using a prototype's name
directly will cause a direct lookup.  Using an extension's (instance's) name
will not.

If that "proxy lookup" fails to find the attribute, then the object itself
will be searched for the attribute as if the proxy object didn't exist.
This happens no matter what prototypes the proxy object has because the
proxy object's prototypes are not consulted.  Thus this proxy action is a
momentary diversion in the attribute lookup logic, and the proxy object is
not actually part of the prototype chain.

Another way of looking at this is that any attributes in the proxy object
will override the same-named object's attributes but only for direct lookups
on the object.  Inherited lookups from an extension will always get the real
object's attributes.

            ---- sample code ----

    object Proto:
            def str_():
                    print 'extension'
            def call_():
                    "special call for instances"
                    blah blah blah

            object attrProxy_:
                    def str_():
                            print 'proto'
                    call_ = defaultCall_

    p = Proto()
    print Proto    #  -->  proto
    print p          #  --> extension

We might want some syntax sugar for "object attrProxy_:".

Note that "p = Proto()" needs attrProxy_.call_() set to "defaultCall_" in
order to free up the Proto namespace for the special Proto.call_.  It would
be nice to somehow make "call = defaultCall_" automatic.  Maybe that could
be part of the syntax sugar for  "object attrProxy_:".
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.